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)
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 and locate relevant files for the ERA001 rule. Let's first see what's in the repo.
- Assistant
Let me explore the repository structure more broadly.
- Assistant
Let me look for ERA001 in the codebase.
- Assistant
Let me look at the ERA001 rule implementation to understand how it works.
- Assistant
Let me understand the issue. The problem is that when you have:
python# ) # ) # noqa: ERA001ERA001 flags the first line as commented-out code. But then RUF100 says the on the second line is unused because ERA001 doesn't flag the second line (since it has a noqa directive suppressing it).
- Assistant
Let me understand the issue:
- Line 1:
# )- This is detected as commented-out code by ERA001. - Line 2:
# ) # noqa: ERA001- This has a# noqa: ERA001directive, but ERA001 doesn't fire on this line because the detection logic considers the entire line as a comment, and when it strips the noqa part, the remaining content is# )which is the same as line 1 and should be detected as code.
The problem is that
is_own_line_commentchecks if the comment is on its own line. The line# ) # noqa: ERA001contains two#characters. Let me look at howis_own_line_commentworks:rustfn is_own_line_comment(line: &str) -> bool { for char in line.chars() { if char == '#' { return true; } if !char.is_whitespace() { return false; } } unreachable!("Comment should contain '#' character") }This function returns
trueas soon as it finds the first#. So it will returntruefor# ) # noqa: ERA001. - Line 1:
- Assistant
Now I understand the issue. Let me trace through the logic carefully:
The issue is with a line like
# ) # noqa: ERA001. Thecomment_contains_codefunction trims the leading#and whitespace, giving) # noqa: ERA001. But this doesn't match the positive cases regex (^[()\[\]{}\s]+$has extra text), andparse_module(") # noqa: ERA001")fails since)alone isn't valid Python. Socomment_contains_codereturnsfalse, meaning ERA001 doesn't fire. Then RUF100 says thenoqa: ERA001is unused.The fix should strip any trailing noqa-like directive from the comment before checking if it contains code.
Let me look at how noqa directives are detected:
- Assistant
- Assistant
Now I understand the issue. Let me trace through the logic:
The issue is that when you have
# ) # noqa: ERA001, thecomment_contains_codefunction:- Trims leading
#and whitespace to get) # noqa: ERA001 - This doesn't match the brackets POSITIVE_CASES regex (because of the extra text)
parse_module(") # noqa: ERA001")fails since)alone isn't valid Python- So ERA001 doesn't fire, and RUF100 reports the noqa directive as unused
The fix should strip inline noqa/allowlist directives from the comment text before checking if it contains code. Let me first reproduce the issue:
- Trims leading
- Assistant
Wait, the issue description says the test case uses tabs. Let me re-read:
python# ) # ) # noqa: ERA001The leading whitespace is tabs and the content after
#also has a tab. Let me be more careful. Actually the playground link might help but I don't have access. Let me just reproduce with:python# ) # ) # noqa: ERA001Where
\tis a tab character. - Assistant
Now let me check if there's a ruff binary available:
- Assistant
Let me check if there's a pre-built ruff binary:
- Assistant
Let me build ruff first:
- Assistant
Now let me test the issue:
- Assistant
I've reproduced the issue. Line 1 triggers ERA001 correctly, but line 2's
# noqa: ERA001prevents code detection, so RUF100 says the noqa is unused.Now let me understand the detection logic more closely:
- Assistant
Let me trace through the exact logic to understand the issue:
For
# \t):- Trim →
) CODE_INDICATORS.is_match(")")→ true (contains))POSITIVE_CASES.is_match(")")→ the brackets regex^[()\[\]{}\s]+$matches → returnstrue✅
For
# \t) # noqa: ERA001:- Trim →
) # noqa: ERA001 CODE_INDICATORS.is_match(") # noqa: ERA001")→ true (contains))ALLOWLIST_REGEX.is_match(") # noqa: ERA001")→ false (starts with), not a directive)POSITIVE_CASES.is_match(") # noqa: ERA001")→ brackets regex fails (has extra text)parse_module(") # noqa: ERA001")→ fails ()alone is not valid Python)- Returns
false❌
The issue: the
# noqa: ERA001at the end of the comment text interferes with code detection, making ERA001 not fire, which then makes RUF100 report the noqa as unused.The fix: strip trailing linter directive comments (like
# noqa: ERA001) from the comment text before checking if it contains code. Let me look at existing tests to understand how to properly fix this: - Trim →
- Assistant
Let me look at the test fixtures to understand the existing test structure.
- Assistant
Let me look at the test file and the snapshot to understand the existing tests.
- Assistant
Let me look at the test snapshot to see which lines are flagged.
- Assistant
I can see that
# "key1": 123, # noqa: ERA001(line 25) is NOT flagged as ERA001, while similar lines without noqa ARE flagged. And#import os # noqa: ERA001(line 15) and#import os # noqa(line 30) are also NOT flagged. Let me check if those also have the RUF100 conflict: - Assistant
Interesting - no RUF100 violations in the existing test file. But
#import os # noqa: ERA001(line 15) and# "key1": 123, # noqa: ERA001(line 25) are also not triggering ERA001, which means they have the same potential issue. Let me check more carefully: - Assistant
So the existing test file already has the same issue! Lines 16, 25, and 30 all have noqa directives that are flagged as unused by RUF100 because ERA001 doesn't fire on those lines. This confirms the bug is widespread.
Now I need to fix
comment_contains_codeto strip inline noqa/linter directives from the comment text before checking for code. Let me look at the SimpleTokenKind to understand the tokenizer: - Assistant
Let me look at the SimpleTokenKind enum to see what kinds are available.
- Assistant
Good,
SimpleTokenKind::Commentexists. Let me look at theis_commentmethod: - Assistant
Let me look at the noqa detection code to see if there's a shared utility for detecting noqa directives:
- Assistant
Now I understand the issue fully. The
comment_contains_codefunction strips leading#and whitespace from the line, so# ) # noqa: ERA001becomes) # noqa: ERA001. This doesn't match any code detection patterns (the# noqa: ERA001suffix prevents it). So ERA001 doesn't fire, and RUF100 says the noqa is unused.The fix should be: in
comment_contains_code, after the initial trim, strip any trailing linter directive comments (like# noqa: ERA001) before checking for code. Let me implement this: - Assistant
Now let me look at how noqa is parsed to understand the pattern better:
- Assistant
Let me look at the lex_inline_noqa function to understand how noqa is detected within comments:
- Assistant
This is very helpful! The
lex_inline_noqafunction already handles the case where there's a noqa directive within a comment range. It skips over leading content until it finds a#, then tries to lex the noqa directive.This means that the noqa directive is already parsed correctly by the noqa lexer. The issue is that
comment_contains_codedoesn't strip the noqa directive before checking if the comment contains code.Let me look more at the noqa lexer to understand how it handles
# comment # noqa: ERA001. - Assistant
Now I fully understand the problem. The
comment_contains_codefunction strips the leading#and whitespace, leaving) # noqa: ERA001. This doesn't start with a directive pattern, so the ALLOWLIST_REGEX check fails. The# noqa: ERA001part prevents the code detection from working.The fix: in
comment_contains_code, after stripping the initial#and whitespace, also strip any secondary inline directive comments (like# noqa: ERA001) from the text before checking for code.Let me implement the fix:
- Assistant
Let me read the rest of the function:
- Assistant
Let me trace through the problem to understand the exact issue.
For the line
# \t) # noqa: ERA001:trim_start_matches(|char| char == '#' || is_python_whitespace(char))strips leading#, tab →) # noqa: ERA001- The ALLOWLIST_REGEX check doesn't match (starts with
), not a directive pattern) - POSITIVE_CASES brackets regex
^[()\[\]{}\s]+$doesn't match (has extra# noqatext) parse_module(") # noqa: ERA001")fails- Returns
false→ ERA001 doesn't fire
The fix: strip trailing inline linter directives (like
# noqa: ERA001) from the comment text before code detection.Let me also check what
is_python_whitespaceincludes: - Assistant
Let me search for the is_python_whitespace function.
- Assistant
Let me see what is_python_whitespace includes.
- Assistant
Good.
is_python_whitespaceis just' ' | '\t' | '\x0C'(space, tab, or form-feed). For mystrip_trailing_directivefunction, I should use the same whitespace definition or just useis_ascii_whitespace()which also includes newline and carriage return, but in practice we're working with a single line so that's fine.Actually, I should use
is_python_whitespacefor consistency. But it's a function that takes achar, and I'm working with bytes. Let me think about the best approach.Actually, looking at the existing code,
line.as_bytes()[i - 1].is_ascii_whitespace()would work becauseis_ascii_whitespace()includes space, tab, newline, carriage return, form feed, and vertical tab. This is a superset of Python whitespace, but that's fine for our purposes.Actually wait, I need to be more careful. The check for whitespace before should match the Python whitespace definition to be consistent. But using is probably fine in practice.
- Assistant
Now I have a clear understanding of the issue. The fix needs to go in
comment_contains_code- after stripping the leading#and whitespace, we should also strip any trailing inline linter directives (like# noqa: ERA001) that appear within the comment text. The# noqa: ERA001is a linter directive that should not be treated as part of the commented-out code.Let me implement the fix:
- Assistant
Let me use the correct syntax for the edit command.
- Assistant
Now I need to implement the fix. I'll add a helper function to strip inline linter directives and call it in
comment_contains_code: - Assistant
Let me verify the patch was applied correctly:
- Assistant
Good, the function is in place. Let me verify the test was added:
- Assistant
Good, the test was added. Let me check the full test:
- Assistant
Now let me build and test to see if everything compiles and the tests pass:
- Assistant
All 14 tests pass. Let me verify the fix works with my test case:
- Assistant
The binary was built before my changes. I need to rebuild:
- Assistant
Good, the build succeeded. Let me test with the issue's example:
- Assistant
The fix works! ERA001 fires on line 1 (correct), and on line 2, ERA001 fires but is properly suppressed by the
# noqa: ERA001directive. The RUF100 unused noqa warning is gone.Now I need to update the test fixture and snapshot to include this case. Let me also check the existing ERA001 test fixture:
- Assistant
The ERA001 diagnostics haven't changed for the existing test file. The RUF100 errors on the noqa lines are now gone because ERA001 now properly fires on those lines, making the noqa directives "used" instead of "unused".
Now let me add a regression test case to the fixture file for the specific issue:
- Assistant
Good. Now I need to add regression test cases for this issue at the end of the file.
- Assistant
Now I need to add regression test cases. Let me add them at the end of the file.
- Assistant
Wait, the issue specifically uses tabs. Let me add the exact case from the issue. Let me first check what we have:
- Assistant
Good. Now I also need to add the tab-based case from the issue. Let me also verify that our ruff handles this correctly:
- Assistant
Good - no RUF100 errors now! The same 19 ERA001 errors as before. The inline noqa directives are now properly handled: ERA001 fires but is suppressed by the noqa, so RUF100 doesn't complain about unused noqa.
Now let me run the snapshot test to see if it still passes:
- Assistant
The snapshot test still passes. Now let me also check if there are any RUF100-related snapshot tests that might need updating:
- Assistant
Let me check if any of the RUF100 snapshots reference ERA001 or the eradicate fixture:
- Assistant
There's a snapshot that references ERA001! Let me check it:
- Assistant
This snapshot is from a different test file (RUF100_5.py). Let me check if this test is affected:
- Assistant
Let me check this file:
- Assistant
Let me look at SimpleTokenKind to understand keywords:
- Assistant
Let me look at the SimpleTokenKind to understand how it classifies tokens:
- Assistant
Let me see more of the SimpleTokenKind:
- Assistant
Let me check if there's a
Namekind: - Assistant
Let me check how the SimpleTokenizer handles keywords. Let me look at its implementation:
- Assistant
Let me check the tokenizer's handling of
#: - Assistant
I see the tokenizer. When it encounters
#, it treats everything until newline as a comment. Withskip_trivia(), the# noqa: ERA001part is skipped. So the consecutive identifiers check on) # noqa: ERA001only sees), which doesn't trigger the consecutive names check.Now let me look at
to_keyword_or_other: - Assistant
Let me check this function to understand how keywords are handled:
- Assistant
I see! The SimpleTokenizer classifies
importasSimpleTokenKind::Import, notSimpleTokenKind::Name. Similarly,fromisSimpleTokenKind::From, etc. Sofrom foo import eradicatewould be tokenized asFrom,Name(foo),Import,Name(eradicate). The consecutive identifiers check looks forNamefollowed byName, butImportis notName. So the check doesn't trigger, and the code continues to the next checks. - Assistant
Let me check the test function:
- Assistant
This test checks both ERA001 and RUF100. Let me run this specific test:
- Assistant
The test passes. Now let me run a broader set of tests to make sure nothing else is broken:
- Assistant
All RUF100 tests pass. Now let me run all eradicate tests:
- Assistant
All 14 eradicate tests pass. Let me also run a broader test to check for any other potential issues:
- Assistant
Let me now also verify the fix with the exact example from the issue (with tab characters):
- Assistant
The issue description shows the input with leading tabs before
#:python# ) # ) # noqa: ERA001But my test file has the same format. Let me check if the exact issue is fixed with these exact characters. Let me also add a test case that matches the issue more exactly with the tab character.
Actually wait, let me first check if the issue is truly fixed by looking at the existing test output I already got:
- Before: ERA001 on line 1, RUF100 on line 2
- After: ERA001 on line 1 only (no RUF100 on line 2)
That's already the correct behavior. Let me verify one more time.
- Assistant
The fix works exactly as expected! ERA001 fires on line 1 (correct), and line 2's
# noqa: ERA001properly suppresses ERA001 so RUF100 doesn't report it as unused. Only 1 error found (ERA001 on line 1), no RUF100 on line 2.Now let me also check for edge cases - what about
# "key1": 123, # noqa: ERA001from the existing fixture? - Assistant
All edge cases work correctly:
- Lines 1, 4, 5: ERA001 fires but is suppressed by noqa → no diagnostic, no RUF100 ✓
- Lines 2, 3, 6: ERA001 fires (these are legitimate commented-out code) ✓
- NO RUF100 errors at all! ✓
Now let me also check if there are any other tests related to noqa/ERA interaction that might be affected:
- Assistant
Let me verify the RUF100_5 snapshot is still correct:
- Assistant
The RUF100_5 snapshot is still correct and passes. Let me now also update the ERA001 snapshot by running the test with
UPDATE_EXPECT=1to make sure it's up to date: - Assistant
Good, the ERA001 snapshot test passes without changes. Let me also make sure the RUF100_5 test is up to date:
- Assistant
All RUF100 tests pass too. Now let me check if there are any other snapshot files that reference the ERA001 fixture file and might need updating:
- Assistant
Good, only one snapshot references eradicate. Let me now also run a broader set of tests to check for any issues:
- Assistant
All tests pass. Let me now verify the complete fix by looking at the diff:
- Assistant
The diff looks clean and focused. Let me now do a final comprehensive check by running all linter tests to make sure nothing else is broken:
- Assistant
2556 tests passed, 0 failed. All tests pass. Let me now do one final verification of the fix with the exact issue example:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.