ISSUE DESCRIPTION Schrödinger's comment: it is both commented-out code and a comment…
- User
ISSUE DESCRIPTION
Schrödinger's comment: it is both commented-out code and a comment that is not code (ERA001 vs RUF100)
Summary
First, I'm impressed by the recent reduction of ERA001 false positives. I suspect that ERA001 is an exceptionally difficult rule to implement.
This is the only time I've seen this situation.
python# ) # ) # noqa: ERA001Found commented-out code (ERA001) [Ln 1, Col 4] Unused
noqadirective (unused:ERA001) (RUF100) [Ln 2, Col 10]tomlpreview = false builtins = [] target-version = "py310" line-length = 88 indent-width = 4 [lint] allowed-confusables = [] dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" extend-select = [] extend-fixable = [] external = [] ignore = [] select = [ "F", "E4", "E7", "E9", "ERA", "RUF" ] [format] indent-style = "space" quote-style = "double"Version
ruff 0.15.14 (9ad2da301 2026-05-21)
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
Assistant
Now let me reproduce the issue. Let me create a test file with the example from the issue.
Assistant
Now let me find the ruff binary or build it. Let me check if there's a pre-built binary.
Assistant
I've reproduced the issue. Now let me understand how ruff extracts noqa directives to see if there's a reusable utility.
Assistant
Let me understand the issue better. The key is that the comment
# ) # noqa: ERA001is a single comment in Python (from first#to end of line). Thecomment_contains_codefunction processes the entire comment text including the# noqa: ERA001part.Let me look at the noqa lexer more carefully to understand how it finds noqa directives within comments.
Assistant
Let me look at how ruff identifies the noqa directive position within acomment.
Assistant
Assistant
Let me look at how the noqa directive is structured, specifically what range it covers. Let me look at the
Directivestruct and how the noqa range is computed.Let me look at the whole noqa directive structure and lex_directive function.
Assistant
Let me look at the
lex_directivefunction to understand how the noqa range is computed, and what the range covers.Assistant
Let me search for noqa-related regex patterns used elsewhere in the codebase.
Assistant
Let me look at the public API of the noqa module and how directives are used elsewhere.
Assistant
Let me look at the
add_noqafunction and how it finds the existing noqa directive within a comment. There might be a helper to extract the noqa range.Actually, let me reconsider the approach. I think the cleanest fix that's self-contained and testable is to strip the noqa directive within
comment_contains_codeusing a regex. Let me look at what regex pattern ruff uses for noqa anywhere in the codebase to be consistent.Assistant
There's a
find_trailing_pragma_offsetfunction inruff_python_trivia::pragmas! This looks exactly like what I need. Let me look at it.Assistant
I found a useful utility:
find_trailing_pragma_offsetinruff_python_trivia::pragmas. This finds the byte offset where a trailing pragma comment (like# noqa: ERA001) starts within a comment.Let me verify my understanding of the issue by tracing through the logic. The core problem is that
comment_contains_codeanalyzes the entire comment text including the# noqa: ERA001directive, which changes the detection result.Let me first verify the current behavior with some test cases.
Assistant
Interesting. Now let me check with RUF100 to see the noqa usage:
Assistant
This confirms my analysis. The inconsistency is clear:
- Line 1
# )→ ERA001 (code), but Line 2# ) # noqa: ERA001→ RUF100 (noqa unused) - Line 5
# hello→ not code, but Line 6# hello # noqa: ERA001→ ERA001 raised (inconsistent!)
The root cause:
comment_contains_codeanalyzes the entire comment including the# noqadirective, which changes detection results. The fix is to strip the trailing pragma before analysis, making# X # pragmabehave like# X.Let me look at the existing test fixtures and snapshots to understand what behavior might change.
- Line 1
Assistant
Let me look at the snapshot file to understand the expected output.
Assistant
Now let me look at the RUF100 (unused noqa) test fixtures, since my fix could change whether noqa directives are considered "used" or "unused".
Assistant
Let me look at the snapshot for RUF100_5 to understand the expected behavior.
Assistant
Now I understand the issue thoroughly. Let me verify my fix won't break the existing RUF100_5 behavior by checking the key lines:
#import os # noqa: ERA001(line 3) → strip →import os→ code (still raised, suppressed by noqa) ✓# "key1": 123, # noqa: ERA001(line 5) → strip →"key1": 123,→ POSITIVE_CASES match → code ✓
My fix preserves all existing behavior while fixing the inconsistency. Let me implement it now.
Assistant
Now let me make the edit. First, update the import:
Assistant
Now let me add the pragma-stripping logic after the initial trim:
Assistant
Now let me rebuild and test:
Assistant
Now let me test against the original issue and the test cases:
Assistant
The fix works perfectly. Let me analyze the results:
Original issue (
test_issue.py):- Before: ERA001 on line 1 + RUF100 on line 2 (the Schrödinger's comment problem)
- After: Only ERA001 on line 1. Line 2's
# noqa: ERA001is now used (ERA001 is raised on line 2 but suppressed by the noqa). No RUF100! ✅ Issue fixed.
Test cases are now all consistent:
# )(code) and# ) # noqa: ERA001(code, suppressed) — consistent ✅# hello(not code) and# hello # noqa: ERA001(noqa unused → RUF100) — consistent ✅# x = 1(code) and# x = 1 # noqa: ERA001(code, suppressed) — consistent ✅# Issue #999(not code) and# Issue #999 # noqa: ERA001(noqa unused → RUF100) — consistent ✅
Now let me run the existing tests to make sure nothing is broken:
Assistant
All eradicate tests pass. Now let me run the snapshot tests for ERA001 and RUF100:
Assistant
Let me check how the ruff rule tests are structured (they may use a different test harness):
Assistant
All RUF100 tests pass. Let me also run the broader test suite to make sure nothing else is affected. Let me run the full ruff rules test suite:
Assistant
All 2554 rule tests pass. Let me also run the noqa-related tests and the full linter test suite to be thorough:
Assistant
All noqa tests pass. Let me run the broader linter test suite to catch any other affected tests:
Assistant
All 2775 tests pass. Let me verify that
comment_contains_codeis only used by ERA001 (to confirm the scope of my change):Assistant
comment_contains_codeis only used by ERA001, so my change is well-scoped. Let me test more edge cases to ensure robustness:Assistant
Let me analyze the edge case results. Most cases work correctly, but I notice
# ) # fmt: skip(line 11) is NOT detected as code, while# )is. This is becausefind_trailing_pragma_offsetusesis_pragma_comment, which doesn't recognizefmt:(unlike eradicate'sALLOWLIST_REGEX). Let me check wherefind_trailing_pragma_offsetis used and whetherfmt:should be included:Assistant
Let me look at how
find_trailing_pragma_offsetis used inoverlong.rs(E501), since that's a similar use case—splitting a comment into content and pragma parts:Assistant
find_trailing_pragma_offsetis the established ruff convention for stripping trailing pragma comments (used by E501 line-length and the formatter). Using it for ERA001 is consistent with the rest of ruff. It handles noqa, type, isort, pylint, nosec, etc.—the directives that can cause the RUF100 "Schrödinger" problem. Thefmt:/mypy:cases are pre-existing inconsistencies (also present in E501) that don't cause RUF100, so they're out of scope.Let me verify the fix handles the key scenarios and check for any broader test impact. Let me search for other test fixtures using ERA001:
Assistant
Only
RUF100_5.pyuses ERA001 outside eradicate fixtures, and that already passes. Let me run clippy to ensure code quality and check for any warnings:Assistant
No clippy warnings. Let me run a final comprehensive verification with the exact issue reproduction and additional edge cases, including non-own-line comments:
Assistant
The fix works perfectly. Let me analyze the results:
-
Exact issue reproduction (lines 2-3): Line 2
# )→ ERA001, Line 3# ) # noqa: ERA001→ no error (ERA001 raised but suppressed by noqa, which is now USED). The Schrödinger's comment problem is fixed — no more RUF100! -
Various pragmas after bracket code are now all consistent:
# } # noqa: ERA001→ suppressed (noqa used) ✓# ] # type: ignore→ ERA001 raised (type:ignore doesn't suppress ERA001) ✓# ( # noqa→ suppressed ✓# ) # pylint: disable=E501→ ERA001 raised ✓
-
Pure pragma comments still correctly not detected as code ✓
-
Non-own-line comments unaffected ✓
Let me clean up the temp files and do a final review of my change:
-
Assistant
The diff is clean and minimal. Let me run the related test suites one final time to confirm everything passes, including the E501 (overlong) tests that also use
find_trailing_pragma_offset:Assistant
All tests pass (eradicate: 13, pycodestyle: 140). My change is isolated to
detection.rs(ERA001 only) and doesn't touch any shared code. Let me do a final confirmation of the changes:Assistant
Only
detection.rsis changed — a minimal, focused fix. Let me check if there's a changelog convention I should follow:Assistant
The changelog entries reference PR numbers I don't have, so I'll skip that. My fix is complete and focused. Let me do a final review of the complete change and submit:
Assistant
The fix is complete and verified. Let me run the full test suite one final time to be absolutely certain there are no regressions:
Assistant
All 2775 tests pass. Let me submit the fix.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.