ruff W605: fix changes program behavior
Hypothesis Nemotron 3 Super predicted to fire fired 1×
file modes and truncation, partial writes, non-str data passed to write
Program
Output
(no stdout) /w/prog.py:2: 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 string literal to a raw string to eliminate the SyntaxWarning about the invalid escape sequence '\_'. The stderr difference (warning disappears) is exactly the semantic change the rule documents as its purpose.
Explanation Nemotron 3 Super unverified
The original string literal '\\_' contains a backslash followed by underscore; Python interprets this as an invalid escape sequence, issuing a SyntaxWarning but treating it as two characters: backslash then underscore. After rewriting to r'\\_', the raw string literal disables escape processing, so the backslash remains a literal character and the underscore is literal—still two characters—but now no warning is emitted. Although the resulting file content is identical, the program’s behavior differs because the SyntaxWarning is suppressed, altering diagnostic output. Since the fix changes whether a warning is produced, it is not semantics‑preserving under Ruff’s definition of safety, which requires identical observable behavior including warnings. Thus labeling the fix safe is incorrect.
Reproduce
- file
prog.py(the before program)- minimized program
- r1-p008.py
- evidence sheet
- r1-p008.md
- regression fixture
- r1-p008.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 sequence · write backslash underscore warning · raw string literal fix
- related astral-sh/ruff/issues/11491 Same rule W605 but different construct (f-string vs regular string literal)
- related astral-sh/ruff/issues/8616 Mentions W605 fixer creating unnecessary escapes, same rule but different context
- related astral-sh/ruff/issues/19655 Interaction between W605 and ISC002 making W605 unfixable, same rule but different defect
- duplicate astral-sh/ruff/issues/11461 Same rule W605, same fix strategy (raw string), same defect class of unsafe fix changing behavior
- related astral-sh/ruff/issues/10434 W605 causing panic on f-string with escapes, same rule but different trigger
- related astral-sh/ruff/issues/6987 W605 autofix error with ANN401, same rule but different construct
- related astral-sh/ruff/issues/5061 Feature request to use raw strings for W605 fix, same rule and fix strategy but not a bug report
- related astral-sh/ruff/issues/19543 Question about W605 on byte strings, same rule but different string type
- related astral-sh/ruff/issues/20196 About emitting all CPython SyntaxWarnings including W605, same rule but broader scope
- related astral-sh/ruff/issues/12165 W605 causing panic on specific file, same rule but different trigger
- related astral-sh/ruff/issues/474 Original implementation issue for W605, same rule but not the same fix defect
- related astral-sh/ruff/issues/8617 Q000 fixer not removing obsolete backslash escapes after quote change, same domain but different rule
- related astral-sh/ruff/issues/18816 PLE2510 fix causing syntax error in f-strings with escapes, similar domain but different rule
- related astral-sh/ruff/issues/14555 UP031 undesirable in raw strings/regex, related to raw string usage but different rule
- related astral-sh/ruff/issues/12391 D301 autofix doubling escaped backslashes in docstrings, similar fix domain but different rule
Details
- program
- File write modes and truncation
- hazard
- file modes and truncation, partial writes, non-str data passed to write
- oracle
stderr before: /w/prog.py:2: SyntaxWarning: invalid escape sequence '\_' stderr after: (none)- run
- 2026-10-17T20-50-05-853Z-2tuo-ruff-W605
generated program (7 lines) and the fix's diff
--- orig.py 2026-10-17 16:50:22.036553753 -0400 +++ fixed.py 2026-10-17 16:50:22.239557691 -0400 @@ -1,5 +1,5 @@ with open('test.txt', 'w') as f: - f.write('\_') + f.write(r'\_') with open('test.txt', 'r') as f: content = f.read() print(repr(content))
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)