Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SyntaxWarning: invalid escape sequence #2184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented May 14, 2024

This removes all those pesky warnings from our unittest output so now it looks like:
% coverage run -m unittest discover tests

............................................................................
.......................ss.............................................s....
----------------------------------------------------------------------
Ran 151 tests in 3.606s

% ruff check --select=W605 --fix # https://docs.astral.sh/ruff

Found 34 errors (34 fixed, 0 remaining).

% ruff rule W605

invalid-escape-sequence (W605)

Derived from the pycodestyle linter.

Fix is always available.

What it does

Checks for invalid escape sequences.

Why is this bad?

Invalid escape sequences are deprecated in Python 3.6.

Example

regex = "\.png$"

Use instead:

regex = r"\.png$"

Or, if the string already contains a valid escape sequence:

value = "new line\nand invalid escape \_ here"

Use instead:

value = "new line\nand invalid escape \\_ here"

References

@dmitryon
Copy link

When running pytests in a freshly configured environment, these warnings lead to test failures.

Steps to reproduce

  1. Create a virtualenv
  2. pip install tweepy
  3. pip install pytest
  4. delete all *.pyc files find . -name \*.pyc -delete
  5. Add test_tweepy_import.py
import tweepy


def test_import():
    assert tweepy.API()
  1. Add pytest.ini
[pytest]
filterwarnings =
    error
    # ignore imghdr deprecation (to be removed in python 3.13)
    ignore:'imghdr' is deprecated.*:DeprecationWarning
  1. Run tests
pytest
========================================================================= test session starts =========================================================================
platform darwin -- Python 3.12.4, pytest-8.2.2, pluggy-1.5.0
rootdir: /tweepy_testfailure
configfile: pytest.ini
collected 0 items / 1 error                                                                                                                                           

=============================================================================== ERRORS ================================================================================
_______________________________________________________________ ERROR collecting test_tweepy_import.py ________________________________________________________________
.venv/lib/python3.12/site-packages/_pytest/python.py:492: in importtestmodule
    mod = import_path(
.venv/lib/python3.12/site-packages/_pytest/pathlib.py:591: in import_path
    importlib.import_module(module_name)
../../.pyenv/versions/3.12.4/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1387: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
.venv/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:178: in exec_module
    exec(co, module.__dict__)
test_tweepy_import.py:1: in <module>
    import tweepy
.venv/lib/python3.12/site-packages/tweepy/__init__.py:12: in <module>
    from tweepy.api import API
E     File "/tweepy_testfailure/.venv/lib/python3.12/site-packages/tweepy/api.py", line 301
E       """home_timeline(*, count, since_id, max_id, trim_user, \
E       ^^^
E   SyntaxError: invalid escape sequence '\['
======================================================================= short test summary info =======================================================================
ERROR test_tweepy_import.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================== 1 error in 0.09s ===========================================================================
~

@dmitryon
Copy link

dmitryon commented Aug 7, 2024

Related issue #2187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants