19

After updating to Python 3.12, I get warnings about invalid escape sequence on some triple-quotes comments.

Is this a new restriction? I have the habit of documenting code using triple-quoted string, but this has never been a problem prior to Python 3.12.

python3 --version
Python 3.12.0
$ ./some_script.py
/some_script.py:123: SyntaxWarning: invalid escape sequence '\d'
  """

I tried replacing all lines with \d:

20230808122708.445|INFO|C:\dist\work\trk-fullstack-test\namespaces.py

with \\d:

20230808122708.445|INFO|C:\\dist\work\trk-fullstack-test\namespaces.py

The warning disappears.

Suppressing the warning do not seem to work:

import warnings
warnings.filterwarnings('ignore', category=SyntaxWarning)

I hope I do not have to escape all Windows paths documented in triplequotes in our code.

2 Answers 2

34

Back in Python 3.6, using invalid escape sequences in string literals was deprecated (bpo-27364). Since then, attempting to use an invalid escape sequence has emitted a DeprecationWarning. This can often go unnoticed if you don't run Python with warnings enabled. DeprecationWarnings are silenced by default.

Python 3.12 upgraded the DeprecationWarning to a SyntaxWarning. SyntaxWarnings are emitted by the compiler when the code is parsed, not when it's being run, so they cannot be ignored using a runtime warning filter. Unlike DeprecationWarnings, SyntaxWarnings are displayed by default, which is why you're seeing it now. This increase in visibility was intentional. In a future version of Python, using invalid escape sequences in string literals is planned to eventually become a hard SyntaxError.

The simplest solution would be to use # comments for comments instead of string literals. Unlike string literals, comments aren't required to follow any special syntax rules. See also the discussion in Python comments: # vs. strings for more on the drawbacks of using string literals as comments.

To address this warning in general, you can make the string literal a raw string literal r"...". Raw string literals do not process escape sequences. For example, the string "\n" contains a single newline character, whereas the string r"\n" contains the two characters \ and n.

Sign up to request clarification or add additional context in comments.

3 Comments

It would appear no longer valid then to put raw-string regular expressions into docstrings, which is a bummer. For sure I can replace re.compile(r"\d+") with re.compile("\\d+") but certainly the problem isn't so much for trivial cases.
@dash-tom-bang I'm not fully sure what you mean. If you're worried escape sequences being expanded inside a docstring, you can use a raw string literal for the docstring.
Horror. Now I'm getting a bunch of messages because of used modules that use such sequences. What am I supposed to do, manually send them corrections? For each module?
16

You can encode your regex string under r'' in Python 3.12.3. It works for me.

In 3.11.4 before : '(?:\d{4}(-)?\d{3}(-)?[8]\d{2}(-)?\d{2})'

In 3.12.3 after : r'(?:\d{4}(-)?\d{3}(-)?[8]\d{2}(-)?\d{2})'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.