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

SyntaxWarning on regexp on first run of black #4188

Closed
Julien-Elie opened this issue Jan 28, 2024 · 2 comments · Fixed by #4189
Closed

SyntaxWarning on regexp on first run of black #4188

Julien-Elie opened this issue Jan 28, 2024 · 2 comments · Fixed by #4189
Labels
T: bug Something isn't working

Comments

@Julien-Elie
Copy link

When running black on the following code:

text = re.sub(
    "([_a-zA-Z0-9-+]+)(\.[_a-zA-Z0-9-+]+)*"
    "@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})",
    '<a href="mailto:\g<0>">\g<0></a>',
    text,
)
text = re.sub(
    "(ftp|http|https):\/\/(\w+:{0,1}\w*@)?"
    "(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?",
    '<a href="\g<0>">\g<0></a>',
    text,
)

I get the following warnings (written twice):

<unknown>:2: SyntaxWarning: invalid escape sequence '\.'
<unknown>:3: SyntaxWarning: invalid escape sequence '\.'
<unknown>:4: SyntaxWarning: invalid escape sequence '\g'
<unknown>:8: SyntaxWarning: invalid escape sequence '\/'
<unknown>:9: SyntaxWarning: invalid escape sequence '\S'
<unknown>:10: SyntaxWarning: invalid escape sequence '\g'
<unknown>:2: SyntaxWarning: invalid escape sequence '\.'
<unknown>:3: SyntaxWarning: invalid escape sequence '\.'
<unknown>:4: SyntaxWarning: invalid escape sequence '\g'
<unknown>:8: SyntaxWarning: invalid escape sequence '\/'
<unknown>:9: SyntaxWarning: invalid escape sequence '\S'
<unknown>:10: SyntaxWarning: invalid escape sequence '\g'

When re-running black on the same file, the warnings are not shown again. I have to modify the lines (adding a space for instance) to see the warnings again.

Are these warnings normal? (The syntax is normally correct according to the documentation of the re module.)
If they are normal, should they really appear twice in the output? And why don't they appear again when running a second time black?

@Julien-Elie Julien-Elie added the T: bug Something isn't working label Jan 28, 2024
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 28, 2024

Thanks for the issue. First, some background on these warnings... The issue has little to do with re, the issue is with the use of any invalid escape sequence in any Python string — it just comes up with re because people use a lot of backslashes in regular expressions.

See https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences , in particular:

Changed in version 3.6: Unrecognized escape sequences produce a DeprecationWarning.
Changed in version 3.12: Unrecognized escape sequences produce a SyntaxWarning. In a future Python version they will be eventually a SyntaxError.

If you run your code with PYTHONWARNINGS=error, it will actually raise an Exception today (and as above, will raise SyntaxError in some future version of Python).

λ python -c 'invalid_escape = "\/"'
λ PYTHONWARNINGS=error python -c 'invalid_escape = "\/"'
  File "<string>", line 1
    invalid_escape = "\/"
                     ^^^^
SyntaxError: invalid escape sequence '\/'

Anyway, with that said, your code is legal today (if deprecated) and your use of deprecated invalid escape sequences isn't really part of Black's business, so Black should probably swallow these warnings.

@Julien-Elie
Copy link
Author

Thanks for your valuable response. I had never noticed that the uses I did of the backslash were invalid escape sequences for Python (contrary to the same uses in some other languages). I wrote them long time ago with Python 2, and it worked (though invalid, but the syntax error was not raised).
I've fixed them now. Thanks a lot, I've learnt something today :)

I agree these warnings should not pop up when using Black. (Seems like it's time for me to also integrate Pyflakes and Pylint to improve the quality of my scripts.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants