ruff W605: fix changes program behavior
Hypothesis Nemotron 3 Super predicted to fire fired 1×
When the string is used in a regex context via re.sub or similar, doubling backslashes changes the number of escape layers, potentially requiring compensatory changes elsewhere in the pattern.
Program
Output
(no stdout) /w/prog.py:1: SyntaxWarning: invalid escape sequence '\.'
(no stdout)
- fired
- 1 before · 0 after
- runs
- each version twice, same file name, no network
Judge Nemotron 3 Ultra advisory intended-change · low
The rule rewrites an invalid escape sequence '\.' to a raw string r'\.' to eliminate the SyntaxWarning, which is the documented purpose of the fix. The program's behavior changes only by removing the deprecation warning, exactly as intended.
Explanation Nemotron 3 Super unverified
The original string literal `'\.'` contains an invalid backslash‑dot escape; Python emits a SyntaxWarning but still treats it as the two‑character string consisting of a backslash followed by a dot. Rewriting it as a raw string `r'\.'` removes the warning, yet the resulting value remains identical—a backslash and a dot—so the program’s observable behavior does not change; only the diagnostic message differs. Thus the fix is safe regarding semantics, though it alters the warning output.
Reproduce
- file
prog.py(the before program)- minimized program
- r1-p007.py
- evidence sheet
- r1-p007.md
- regression fixture
- r1-p007.py the minimized program with a header stating the observed change, for the tool's test suite
python3 prog.py ruff check --preview --isolated --select W605 --fix prog.py python3 prog.py
Upstream search GitHub issues Tavily Nemotron 3 Super + Ultra
W605 fix · W605 regex pattern backslash dot · invalid escape sequence warning fix · raw string literal prefix addition
- related astral-sh/ruff/issues/11491 Existing report is about W605 fix in f-strings, new finding is a plain string literal.
- duplicate astral-sh/ruff/issues/11461 Existing report 'W605 fix is unsafe' describes the same defect: autofix converts '\.' to raw string r'\.' which changes regex meaning.
- related astral-sh/ruff/issues/10434 W605 panic on f-string with backslashes, different construct (f-string vs plain string).
- related astral-sh/ruff/issues/6987 ANN401/W605 autofix error, but not the same raw-string behavior-change defect.
- related astral-sh/ruff/issues/5061 Feature request to use raw strings for W605 fix, which is what the new finding demonstrates going wrong.
- related astral-sh/ruff/pull/5352 PR implementing raw-string fix for W605, which introduced the defect in the new finding.
Details
- program
- Regex substitution backfire
- hazard
- When the string is used in a regex context via re.sub or similar, doubling backslashes changes the number of escape layers, potentially requiring compensatory changes elsewhere in the pattern.
- oracle
stderr before: /w/prog.py:1: SyntaxWarning: invalid escape sequence '\.' stderr after: (none)- run
- 2026-10-17T20-50-05-853Z-2tuo-ruff-W605
generated program (10 lines) and the fix's diff
--- orig.py 2026-10-17 16:50:21.723547676 -0400 +++ fixed.py 2026-10-17 16:50:21.933551754 -0400 @@ -1,5 +1,5 @@ import re -pattern = '\.' +pattern = r'\.' text = 'a.b' result = re.sub(pattern, 'X', text) print(result)
rule documentation given to the models
# invalid-escape-sequence (W605) Derived from the **pycodestyle** linter. Fix is always available. ## What it does Checks for invalid escape sequences. ## Why is this bad? Invalid escape sequences are deprecated in Python 3.6. ## Example ```python regex = "\.png$" ``` Use instead: ```python regex = r"\.png$" ``` Or, if the string already contains a valid escape sequence: ```python value = "new line\nand invalid escape \_ here" ``` Use instead: ```python value = "new line\nand invalid escape \\_ here" ``` ## References - [Python documentation: String and Bytes literals](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals)