A backslash in a string starts no escape sequence

tx0/python:invalid-escapedefaultwarningbug

What it finds

"\d+": a backslash before a character that starts no escape sequence is kept as it is, but Python 3.6 deprecated this, 3.12 turned the warning into a SyntaxWarning shown on every import, and a later version will make it an error. Such strings are usually regular expressions or Windows paths that were meant to be raw.

The fix doubles each such backslash, which keeps the value of the string exactly, and is safe.

From the rule's source, invalid_escape.rs.

Example from the rule's tests
import re PATTERN = re.compile("\d+\.\d+")tx0 reports: \d is not an escape sequence, which Python warns aboutPATH = "C:\temp\new"RAW = r"\d+"BYTES = b"\q"tx0 reports: \q is not an escape sequenceUNICODE = "\N{BULLET} \u00e9 \x41 \101"FORMATTED = f"{RAW}\s"tx0 reports: \s is not an escape sequence
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:invalid-escape" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:invalid-escape] reason="..."

tx0 explain tx0/python:invalid-escape shows the same in the terminal. Found a false positive? Report it.