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

W605 error while checking docstrings with regex inside it (again) v.2.5.0 #854

Closed
AlexArcPy opened this issue Mar 13, 2019 · 4 comments
Closed

Comments

@AlexArcPy
Copy link

This is the same issue as #822 and #795, it is giving the same error.

import re
def test_regex_function():
    """
    Return compiled regex of which allows to process only specific file
    paths or extensions, e.g. r"^.*\.svg$".
    :return:
    """
    return re.compile(r"^.*\.svg$")

Error:

W605 invalid escape sequence '\.'

Version:

$ pycodestyle --version
2.5.0

It was supposed to be fixed by #818 and merged to the master. However, the error persists. Any workarounds?

@asottile
Copy link
Member

The lint is correct here, that docstring will become a syntax error in 3.8

You want to prefix your docstring with an r to make it a raw string or escape your backslash

The linked PR only corrects the position of the error message

If you'd like a tool which will autofix this, one of the automatic rewrites of pyupgrade covers this

@AlexArcPy
Copy link
Author

@asottile ah thanks for the clarification. So, in 3.8 this docstring would also give a syntax error?

def foo():
  """add C:\Program Files\Git\bin on Windows to the PATH"""
  pass

Currently giving

W605 invalid escape sequence '\P'
W605 invalid escape sequence '\G'

@asottile
Copy link
Member

yes, that one will as well

in fact today it produces what you probably wouldn't expect due to \b:

>>> foo.__doc__
'add C:\\Program Files\\Git\x08in on Windows to the PATH'
>>> print(foo.__doc__)
add C:\Program Files\Giin on Windows to the PATH

If you want to see the py38 behaviour and have access to py36 / py37 you can use:

$ python3.7 -Werror -c '"""foo\qar"""'
  File "<string>", line 1
SyntaxError: invalid escape sequence \q

@AlexArcPy
Copy link
Author

Ah brilliant, thanks a ton for clarifying!

has2k1 added a commit to has2k1/plydata that referenced this issue Jun 30, 2019
Need to escape the single slash in docstring regexes.
It is an error in Python 3.8

See PyCQA/pycodestyle#854 for
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

No branches or pull requests

2 participants