Skip to content

Issues with Python escaping in test_traits.py #3973

@andy-maier

Description

@andy-maier

Description

The test module test_traits.py has two issues with Python escaping:

test_traits.py, lines 60 and 61:

        r'var(--my-color-\\1234)', # CSS variable escaped hex character
        'var(--my-color-\.)', # CSS variable with escaped characters

Line 60 probably has the issue that \\ in a raw string results in two \ characters. I don't think this is what is intended here. The comment suggests the intention is to specify an escaped hex character, which would be:

        r'var(--my-color-\x1234)', # CSS variable escaped hex character

Line 61 has the issue that \ in a non-raw string is interpreted as a Python escape, along with the following character .. This is obviously not a valid Python escape character, and Python issues a SyntaxWarning:

$ python
Python 3.12.7 (main, Oct  1 2024, 02:05:46) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'var(--my-color-\.)'
<stdin>:1: SyntaxWarning: invalid escape sequence '\.'

I don't think this is what is intended here, but I'm not sure what the solution would be. Maybe just specifying this as a raw string:

        r'var(--my-color-\.)', # CSS variable with escaped characters

Reproduce

For reproducing the SyntaxWarning, see above.

I found this when checking for missing dependencies using the 'pip-missing-reqs' command from the 'pip-check-reqs' package:

  1. Create and activate a fresh Python virtual environment
mkvirtualenv ipy312 -p python3.12
  1. Create a dependencies file:
cat >deps.txt << EOF
ipywidgets
pip-check-reqs
jsonschema
pytest
traitlets
ipython
comm
ipykernel
EOF
  1. Install packages:
pip install -r deps.txt
  1. Check for missing dependencies:
pip-missing-reqs $(python -c "import ipywidgets as m, os; dm=os.path.dirname(m.__file__); print(dm)") --requirements-file=deps.txt
...../site-packages/ipywidgets/widgets/tests/test_traits.py:61: SyntaxWarning: invalid escape sequence '\.'
  'var(--my-color-\.)', # CSS variable with escaped characters

Expected behavior

No SyntaxWarning

Context

  • ipywidgets version: 8.1.5
  • Operating System and version: macOS 14.7.2
  • Browser and version: N/A
Troubleshoot Output
(ipywidgets312) maiera-mbp-3:ipywidgets maiera$ jupyter troubleshoot
$PATH:
	/Users/maiera/virtualenvs/ipywidgets312/bin
	/Users/maiera/local/bin
	/opt/homebrew/bin
	/opt/homebrew/sbin
	/usr/local/bin
	/System/Cryptexes/App/usr/bin
	/usr/bin
	/bin
	/usr/sbin
	/sbin
	/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin
	/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin
	/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

sys.path:
/Users/maiera/virtualenvs/ipywidgets312/bin
/opt/homebrew/Cellar/python@3.12/3.12.7_1/Frameworks/Python.framework/Versions/3.12/lib/python312.zip
/opt/homebrew/Cellar/python@3.12/3.12.7_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12
/opt/homebrew/Cellar/python@3.12/3.12.7_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload
/Users/maiera/virtualenvs/ipywidgets312/lib/python3.12/site-packages

sys.executable:
/Users/maiera/virtualenvs/ipywidgets312/bin/python

sys.version:
3.12.7 (main, Oct 1 2024, 02:05:46) [Clang 15.0.0 (clang-1500.3.9.4)]

platform.platform():
macOS-14.7.2-arm64-arm-64bit

which -a jupyter:
/Users/maiera/virtualenvs/ipywidgets312/bin/jupyter

pip list:
Package Version
------------------------- -----------
appnope 0.1.4
asttokens 3.0.0
attrs 25.1.0
comm 0.2.2
debugpy 1.8.12
decorator 5.1.1
executing 2.2.0
iniconfig 2.0.0
ipykernel 6.29.5
ipython 8.31.0
ipywidgets 8.1.5
jedi 0.19.2
jsonschema 4.23.0
jsonschema-specifications 2024.10.1
jupyter_client 8.6.3
jupyter_core 5.7.2
jupyterlab_widgets 3.0.13
matplotlib-inline 0.1.7
nest-asyncio 1.6.0
packaging 24.2
parso 0.8.4
pexpect 4.9.0
pip 24.3.1
pip-check-reqs 2.5.3
platformdirs 4.3.6
pluggy 1.5.0
prompt_toolkit 3.0.50
psutil 6.1.1
ptyprocess 0.7.0
pure_eval 0.2.3
Pygments 2.19.1
pytest 8.3.4
python-dateutil 2.9.0.post0
pyzmq 26.2.0
referencing 0.36.2
rpds-py 0.22.3
six 1.17.0
stack-data 0.6.3
tornado 6.4.2
traitlets 5.14.3
typing_extensions 4.12.2
wcwidth 0.2.13
widgetsnbextension 4.0.13

Command Line Output
N/A
Browser Output
N/A

If using JupyterLab

  • JupyterLab version: N/A
Installed Labextensions
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions