Skip to content

invalid-escape-sequence

Pythonsuspiciousno fixdefault: warnpython/ruff

Reports a backslash in a string that is not raw and does not start an escape sequence ("\d", "\_"), also in the pieces of an f-string.

The language keeps the backslash and the letter, so "\d" works today, but a later version can give the sequence a meaning, and newer interpreters already warn or reject it. It is also usually a regular expression or a Windows path where the author meant a different escape.

To fix, double the backslash ("\\d") or make the string raw (r"\d").

Reported
x = "\d"
y = r"\d"
z = b"\d"
f = f"\d {x} \w {x:\d} \{x}"
g = "\u1234 \x41 \101 \n \\ \' \N{EM DASH}"
h = "\8" "\_"
i = """
\d
"""
python:
lint:
rules:
invalid-escape-sequence: warn # off, warn (default), error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line invalid-escape-sequence in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff W605 full