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.
# ) # ) # noqa: ERA001Found commented-out code (ERA001) [Ln 1, Col 4] Unused
noqadirective (unused:ERA001) (RUF100) [Ln 2, Col 10]preview = 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:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Let me understand the issue more deeply. First, let me reproduce the issue.
Assistant
Let me build the project first and then test.
Assistant
Now let me create a test script to reproduce the issue:
Assistant
Let me build ruff:
Assistant
I've reproduced the issue. Now let me look more carefully at the detection code:
Assistant
Let me test some edge cases to understand the behavior:
Assistant
Let me test more cases:
Assistant
Interesting! So
# ) # noqa: ERA001does NOT trigger ERA001. Let me trace through why.Line:
# ) # noqa: ERA001- After trim:
) # noqa: ERA001 - CODE_INDICATORS: contains
). Yes. - Tokenizer:
)(Punct), then comment# noqa: ERA001. Only one token, doesn't return false. - ALLOWLIST_REGEX: line starts with
), not with the patterns. Doesn't match. - HASH_NUMBER: regex
#\d. The line has# noqawhich is#followed by, not a digit. So no match. - POSITIVE_CASES:
^[()\[\]{}\s]+$- the line is) # noqa: ERA001. The bracket is), then spaces, then# noqa: ERA001. The regex requires the entire line to be brackets and whitespace. So no. - parse_module(
) # noqa: ERA001): does this parse? Let me test...
Actually, let me check what happens with parse_module on
) # noqa: ERA001. If it parses successfully, thencomment_contains_codereturns true (which is the case for# )). But our test shows it doesn't trigger ERA001.Let me test:
- After trim:
Assistant
Let me add a test in the detection code to see what happens:
Assistant
Let me just look at what the existing tests show and create a more direct test:
Assistant
Let me look at the original code to understand the logic. I want to understand why
# ) # noqa: ERA001is NOT detected as code but# \t) # noqa: ERA001IS detected.Assistant
Let me look at the issue more carefully. The problem appears to be that when the line contains a tab before the code,
parse_modulehandles it differently:Assistant
Let me run the exact test case from the issue:
Assistant
Let me look at how RUF100 detects unused noqa:
Assistant
OK let me look at the suppression visitor that actually finds unused noqa.
Assistant
Let me look at the checkers to see how suppression comments are tracked:
Assistant
Let me create a small Rust test to check what
parse_moduledoes for these inputs:Assistant
Let me check the case without the tab:
Assistant
Let me look at the test file more carefully:
Assistant
Let me run the actual test:
Assistant
Now I understand the issue. Let me trace through the logic:
For
# \t) # noqa: ERA001:- After trim:
) # noqa: ERA001 - The various checks pass (not in allowlist, not HASH_NUMBER, etc.)
- POSITIVE_CASES:
^[()\[\]{}\s]+$doesn't match (because of# noqa: ERA001) parse_module(") # noqa: ERA001")fails (because)is a syntax error)- So
comment_contains_codereturnsfalse, ERA001 doesn't fire - But the noqa is on the line, and since no ERA001 fires, RUF100 says the noqa is "unused"
The fix is: if the line has a
# noqadirective and the part before it would be code, we should detect it as code (so ERA001 fires, gets suppressed by the noqa, and RUF100 sees the noqa as "used").Let me check the current logic to understand the fix better:
- After trim:
Assistant
Now let me look at the detection function again to figure out the right approach:
Assistant
Let me test the parsing for some edge cases:
Assistant
Now let me implement the fix. The cleanest approach is to add a check in
comment_contains_codeto handle the noqa case:Assistant
Let me look at the changelog structure:
Assistant
Now let me look at the test file to understand the existing test patterns:
Assistant
Now let me implement the fix. I'll add a check in
comment_contains_codeto handle the case where a noqa directive is at the end of a commented-out code line:Assistant
Let me look at how the noqa check works:
Assistant
Now I'll implement the fix. The plan is to make
comment_contains_codereturntruefor lines that have a# noqadirective with code before it, so that ERA001 fires and the noqa properly suppresses it.Assistant
Now let me build and test:
Assistant
The fix works! Now only one error is reported (ERA001 on line 1). The noqa on line 2 is now used to suppress the ERA001 on line 2 (which the user doesn't see). Let me verify with more cases:
Assistant
Now let me run the existing tests to make sure I didn't break anything:
Assistant
All existing tests pass. Now let me add some new test cases to the ERA001.py fixture:
Assistant
The test failed because of the new test cases I added. Let me update the snapshot:
Assistant
The new snapshot looks correct. Let me fix the test fixture - the
def bar():doesn't have a body which causes a syntax error:Assistant
The fix is working! The test fixture is just not syntactically valid Python. Let me restructure:
Assistant
The new snapshot correctly shows that:
- Line 115 (
# )) fires ERA001 - Line 116 (
# ) # noqa: ERA001) does NOT fire ERA001 (the noqa suppresses it)
Let me accept the new snapshot:
- Line 115 (
Assistant
All tests pass. Now let me also add unit tests for the detection function to make sure my fix is well-tested:
Assistant
Now let me add the test cases to the detection.rs file:
Assistant
Let me try a different approach using a heredoc:
Assistant
All tests pass. Now let me also test the issue's exact example with the more complex test fixture I added:
Assistant
Let me test the exact case from the issue:
Assistant
The fix is working as expected. Let me run broader tests to make sure nothing is broken:
Assistant
All 2776 tests pass. Let me also run the broader integration tests:
Assistant
All tests pass. Let me test some additional edge cases:
Assistant
Let me check what happens with
#import os # noqa: ERA001:Assistant
Let me check why RUF100 doesn't fire on line 6 (
#import os # noqa: ERA001):Assistant
I see. The
is_code_usedcheck is:- If file-level: check if any diagnostic has the code.
- Otherwise: check if the noqa suppressed any diagnostic with the code.
For line 6 (
#import os # noqa: ERA001):- The noqa is for ERA001.
- No diagnostic is suppressed (because no ERA001 fires).
- So
is_code_usedis false. - But the output doesn't show RUF100 for this line.
Let me check more carefully.
Oh wait, maybe the issue is that for line 6, the noqa is not actually extracted. Let me check.
Assistant
Assistant
Still no RUF100. Let me check the noqa detection logic more carefully.
Actually, wait. I think the issue is that the comment_contains_code function returns false for
#import os # noqa: ERA001(because of the two consecutive names check), but the noqa IS still considered as "used" because of how the noqa check works.Let me look at the noqa check more carefully. Maybe the noqa is being considered as "used" for some other reason.
Actually, let me check if the noqa is even being detected. Maybe the parser is not finding the noqa.
Assistant
Let me check the actual noqa processing for this line:
Assistant
Interesting! When I run with
--select RUF(not RUF,ERA), RUF100 fires with "non-enabled:ERA001". This is strange.Wait, let me re-look at the previous output. When I ran with
--select ERA,RUFand--select ERA, the noqa was not flagged. Let me re-test:Assistant
With both ERA and RUF selected, the noqa is not flagged. This is because:
- When ERA is enabled, the noqa check considers the noqa as "used" because... hmm.
Wait, I think I understand now. The noqa is checked against the actual diagnostics. If the comment_contains_code function returns false, no ERA001 diagnostic is generated. But the noqa is still extracted from the line. The noqa check looks at all diagnostics and sees if the noqa suppressed any of them.
If no diagnostic is generated, the noqa is "unused" (because there's nothing to suppress). So RUF100 should fire.
But the output shows no RUF100 when ERA is selected. Let me check the noqa check logic again.
Actually, I think there might be something special about how the noqa is considered "used" for commented-out code. Let me search.
Actually, let me look at the suppression code. Maybe there's special handling for ERA001.
Assistant
Let me check the issue more carefully. The previous test with
--select ERA,RUFshowed no RUF100, but--select RUFdid show RUF100. Let me check if there's something about the test setup:Assistant
Let me look more carefully at what happens when ERA is selected. The behavior is different from when it's not. Let me add some debug:
Assistant
Let me check if the noqa is being detected:
Assistant
Let me check the noqa check for the actual case:
Assistant
Let me dig into the actual behavior. Let me check if there's something specific about how noqa works on lines with two
#:Assistant
OK so:
- For
# test # noqa: ERA001(one comment, contains noqa), RUF100 doesn't fire (noqa is considered as used? Or the noqa is not detected?) - For
# test\n# noqa: ERA001(two comments, second has noqa), RUF100 fires (noqa is unused)
So the difference is that the noqa in a single comment is not flagged as unused. This is the case in the original issue.
So for
# ) # noqa: ERA001, the noqa is in the same comment as the). The noqa is "used" because... why?Let me check. Maybe the noqa check has a special case for noqa inside a comment that is part of a single line.
Wait, the noqa is inside a comment. The comment is
# ) # noqa: ERA001. The noqa is# noqa: ERA001. The noqa IS detected as a noqa directive (the noqa parsing code finds it).But the noqa check doesn't fire. Why?
Let me check the noqa parsing code.
- For
Assistant
Let me investigate the difference more. Let me try a few cases:
Assistant
Let me revert my fix temporarily to check the original behavior:
Assistant
OK so the original behavior is:
- Test 1:
# ) # noqa: ERA001- RUF100 fires (noqa unused) - this is the issue - Test 2:
# test # noqa: ERA001- no error (noqa is somehow considered as used) - Test 4:
# this is a test # noqa: ERA001- RUF100 fires (noqa unused)
Why is test 2 different? Let me check the parse_module behavior:
- Test 1:
Assistant
Now I understand! The original behavior is:
testparses successfully, so# test # noqa: ERA001is detected as code, ERA001 fires and is suppressed, no RUF100.)fails to parse, so# ) # noqa: ERA001is NOT detected as code, no ERA001 fires, RUF100 says noqa is unused.
My fix addresses this case. Let me re-apply my fix:
Assistant
With my fix:
- Test 1:
# ) # noqa: ERA001- All checks passed (no error) - Test 2:
# test # noqa: ERA001- All checks passed (no error) - Test 4:
# this is a test # noqa: ERA001- RUF100 fires (correct, this is a comment, not code)
Now let me run the broader test suite to make sure nothing is broken:
- Test 1:
Assistant
The fix is working correctly. Let me also check that the test fixture is still valid:
Assistant
All tests pass. Now let me also verify by running the whole linter test suite once more:
Assistant
Now let me also test a few more edge cases to make sure the fix is robust:
Assistant
Let me test some more edge cases:
Assistant
The fix is working correctly. Let me also run a broader test to make sure no other tests are broken:
Assistant
All tests pass. Now let me also add a changelog entry:
Assistant
Let me check my final state and make sure everything is clean:
Assistant
I accidentally removed the previous test fixture. Let me add it back:
Assistant
Let me run the full test suite one more time:
Assistant
All 2776 tests pass. Let me also check the exact case from the issue:
Assistant
The fix is working correctly. Let me also clean up the temp files and do a final verification:
Assistant
Let me now review the final fix and verify everything one more time:
Assistant
All tests pass. Let me also run the broader test suite one more time:
Assistant
All 2776 tests pass. Let me now write a brief summary of the changes and verify the checklist:
Summary
I fixed the "Schrödinger's comment" issue described in the bug report. The problem was that when a line like:
# ) # ) # noqa: ERA001was checked, both
ERA001(commented-out code) andRUF100(unused noqa) would fire on the same logical code unit, creating a contradictory state.Assistant
The changes are complete. Let me also remove the
assertion_line: 23from the snapshot since it was added by the test runner and is not part of the meaningful snapshot:Assistant
The
assertion_lineis a standard part of the insta snapshot. Let me do a final verification with the original issue:Assistant
The fix is working correctly. The contradictory state described in the issue is resolved:
Before fix:
- Line 1: ERA001 fires
- Line 2: RUF100 fires (noqa unused)
After fix:
- Line 1: ERA001 fires (the user wants this)
- Line 2: No errors (the noqa properly suppresses ERA001, no RUF100 contradiction)
Assistant
The fix is complete. Let me provide a summary of the changes and my reasoning for the checklist items:
Summary
I fixed the "Schrödinger's comment" issue. The problem occurred when a user added a
# noqa: ERA001directive to a line containing commented-out code, like:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.