fixhunt

ruff W605: fix changes program behavior

fix-changes-behaviorlabelled safeintended-change · lowduplicate
Findings / 2026-10-17T20-50-05-853Z-2tuo-ruff-W605 / r1-p008ruff 0.17.17 → 2 lines2026-10-17 20:50 UTC

Hypothesis Nemotron 3 Super predicted to fire fired 1×

file modes and truncation, partial writes, non-str data passed to write

Program

before
1with open('test.txt', 'w') as f:
2 f.write('\_')
after
1with open('test.txt', 'w') as f:
2 f.write(r'\_')

Output

stdout beforeexit 0
(no stdout)
/w/prog.py:2: SyntaxWarning: invalid escape sequence '\_'
stdout afterexit 0
(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

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
1with open('test.txt', 'w') as f:
2 f.write('\_')
3with open('test.txt', 'r') as f:
4 content = f.read()
5print(repr(content))
6import os
7os.remove('test.txt')
--- 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)