ruff W605: fix changes program behavior
Hypothesis Nemotron 3 Super predicted to fire fired 1×
If the string contains mixed valid and invalid escapes (e.g., "\n\_"), fixing only the invalid part may create an inconsistent interpretation where the valid escape remains unchanged but the invalid o
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 the invalid escape sequence '\_' to the valid escaped backslash '\\_', which changes the string value from a newline followed by '_' to a newline followed by '\_'. This is exactly the semantic change the rule documents: eliminating the deprecated SyntaxWarning by making the escape sequence valid, even though the resulting string content differs.
Explanation Nemotron 3 Super unverified
The original string literal contains an invalid escape sequence `\_` which Python treats as two characters backslash and underscore while emitting a warning; replacing it with `\\_` yields a single escaped backslash followed by underscore changing the runtime value from `'\\_'` to `'\\\\_'`. This alters the string’s content so the program’s behavior differs.
Reproduce
- file
prog.py(the before program)- minimized program
- r1-p004.py
- evidence sheet
- r1-p004.md
- regression fixture
- r1-p004.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 invalid escape backslash underscore · ruff W605 escape sequence warning fix · invalid escape \_ to \\_ correction
- related astral-sh/ruff/issues/11491 Existing report is about W605 fix in f-strings; new finding is a plain string literal.
- related astral-sh/ruff/issues/8616 Mentions W605 fixer creating unnecessary escapes; same rule but different context.
- duplicate astral-sh/ruff/issues/11461 Existing report 'W605 fix is unsafe' describes the same defect: the autofix changes '\_' to '\\_' in a plain string, altering runtime behavior.
- related astral-sh/ruff/issues/10434 W605 panic in f-strings; same rule but different trigger.
- related astral-sh/ruff/issues/6987 Autofix error involving W605; same rule but different construct.
- related astral-sh/ruff/issues/5061 Feature request to use raw strings for W605; same rule but not the same defect.
- related astral-sh/ruff/issues/19543 About W605 on byte strings; same rule but different string type.
- related astral-sh/ruff/issues/5352 PR for raw-string fix for W605; same rule but not the same defect.
Details
- program
- Mixed valid and invalid escapes
- hazard
- If the string contains mixed valid and invalid escapes (e.g., "\n\_"), fixing only the invalid part may create an inconsistent interpretation where the valid escape remains unchanged but the invalid o
- 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 (5 lines) and the fix's diff
--- orig.py 2026-10-17 16:50:20.778529321 -0400 +++ fixed.py 2026-10-17 16:50:20.992533481 -0400 @@ -1,4 +1,4 @@ -s = '\n\_' +s = '\n\\_' print(repr(s)) print(len(s)) for c in s:
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)