fixhunt

ruff W605: fix changes program behavior

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

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

before
1s = '\n\_'
after
1s = '\n\\_'

Output

stdout beforeexit 0
(no stdout)
/w/prog.py:1: 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 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

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
1s = '\n\_'
2print(repr(s))
3print(len(s))
4for c in s:
5 print(ord(c))
--- 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)