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
Assistant
Let me first reproduce the issue by creating a test file and running ruff on it.
Assistant
I've reproduced the issue. Now let me verify the behavior without the noqa directive
Assistant
Now I understand the issue better. Without the noqa, both lines get ERA001. With the noqa, ERA001 is suppressed on line 2 but RUF100 says it's unused. This means RUF100 incorrectly determines that ERA001 wouldn't have fired on line 2 even without the noqa.
Let me find the source code for both ERA001 and RUF100:
Assistant
Let me look at the commented_out_code.rs file to understand the ERA001 rule.
Assistant
The open command requires a special command format. Let me try again.
Assistant
Let me also check the detection logic and the RUF100 rule.
Assistant
Let me look at the detection.rs file to understand how comment_contains_code works.
Assistant
Let me read the rest of the detection.rs file
Assistant
Let me verify what happens with ERA001 on the second line alone:
Assistant
Let me test ERA001 with just the same line but without noqa keyword
Assistant
Let me trace through the function more carefully. Let me check what
parse_moduledoes with) # noqa: ERA001Assistant
So
) # noqa: ERA001doesn't parse as Python code. But)alone would also not parse. Let me check:Assistant
Let me find the RUF100 rule implementation.
Assistant
Let me look at the unused_noqa.rs file
Assistant
Let me look at the noqa checker logic
Assistant
Let me read the rest of the noqa checker:
Assistant
Let me see the full flow. Let me check the
comment_contains_codefunction more carefully.Assistant
Let me look at how noqa directives are processed. Let me search for noqa parsing code:
Assistant
Let me look at the suppression.rs and noqa module to understand how noqa directives are processed:
Assistant
Let me find the noqa module:
Assistant
Let me look at the noqa.rs module for directive parsing:
Assistant
Now I have a clear picture of the issue. Let me look at the ALLOWLIST_REGEX more carefully to understand the patterns:
Assistant
Let me look at how ruff identifies noqa directives to understand the pattern better:
Assistant
Let me look at the
lex_inline_noqafunction for understanding how noqa is parsed:Assistant
Let me check the existing tests and think about the fix. Let me look for existing ERA001 tests related to noqa:
Assistant
Let me look at the test fixtures for ERA001:
Assistant
Let me look at the ERA001 test fixture:
Assistant
Let me test if ERA001 fires on these existing test cases:
Assistant
Let me check the specific behavior for the
# "key1": 123, # noqa: ERA001case:Assistant
Let me check without the noqa:
Assistant
Let me write a small test to understand what's happening in the detection logic:
Assistant
Let me check the
is_own_line_commentfunction:Assistant
Let me see the
is_own_line_commentfunction:Assistant
Let me add a temporary debug test to verify the behavior:
Assistant
Let me first revert that and then look at the end of the detection.rs file:
Assistant
Let me test the complete ERA+RUF behavior with the test fixture:
Assistant
Now I have a clear understanding of the issue. Let me trace through the problem:
For
# ) # noqa: ERA001:- After stripping:
) # noqa: ERA001 - The POSITIVE_CASES pattern
^[()\[\]{}\s]+$doesn't match because of the trailing# noqa: ERA001 parse_modulealso fails because)alone doesn't parse- So
comment_contains_codereturns false → ERA001 doesn't fire - RUF100 sees the noqa directive is unused
The fix needs to strip tool directives (like
# noqa: ERA001) from the end of the line before checking for code. Let me implement this.- After stripping:
Assistant
Now let me implement the fix. Let me first look at the complete function and then modify it:
Assistant
Now let me implement the fix. I'll modify the
comment_contains_codefunction to strip trailing tool directives (like# noqa: ERA001) before checking for code.First, let me look at how to properly detect tool directives:
Assistant
Now I'll implement the fix. The approach is to strip trailing tool directives (like
# noqa: ERA001) from the line before checking for code. Let me edit the detection.rs file:Assistant
Now let me modify the
comment_contains_codefunction to strip trailing tool directives:Assistant
Let me fix the duplicate doc comment:
Assistant
Now let me compile and test:
Assistant
Great, it compiles! Now let me test with the original issue case:
Assistant
The RUF100 error is gone! Now ERA001 on line 2 is properly detected and suppressed by the noqa. Let me test with the existing test fixture:
Assistant
The output is the same as before for the existing test fixture. No RUF100 violations introduced. Let me now run the existing tests:
Assistant
All detection tests pass. Let me also run the broader eradicate tests:
Assistant
All tests pass. Now let me test some edge cases to make sure the fix is correct:
Assistant
The edge cases look correct. Let me verify that
# try: # with commentstill works (non-tool-directive trailing comments should still be flagged):Assistant
The behavior looks correct. Let me also test
# pylint:directive specifically to make sure it strips correctly:Assistant
Let me test line 3 alone:
Assistant
Let me check if
def foo():is valid Python:Assistant
Let me check
# def foo():without the pylint directive (before my fix, this was the behavior too):Assistant
Let me run a broader set of tests to make sure nothing is broken:
Assistant
Let me run the RUF100 tests:
Assistant
Let me run the full test suite for the linter:
Assistant
Let me check if noqa without space is recognized:
Assistant
Let me check if
#noqa(without space) is recognized:Assistant
Let me test parsing
x = 1# noqa: ERA001:Assistant
Let me re-run the test more carefully:
Assistant
Let me check if there are tests for
# regionpatterns:Assistant
The existing
# regiontests should still pass. Let me verify:Assistant
Let me review the final state of my changes:
Assistant
All tests pass. Let me also run a broader test to make sure I haven't broken anything else:
Assistant
Let me verify the exact scenario from the issue one more time:
Assistant
The fix works correctly! Let me summarize the changes:
-
Added
TOOL_DIRECTIVE_REGEX: A new regex that matches the same tool directive patterns asALLOWLIST_REGEXbut without the^anchor (so it can match patterns anywhere in the string). -
Modified
comment_contains_code: After stripping the leading#and whitespace, the function now looks for a trailing#character that is preceded by whitespace. If the content after that#matches a known tool directive (likenoqa,pylint,region, etc.), it strips that trailing directive before analyzing the line for code.
This ensures that tool directives like
# noqa: ERA001at the end of a comment line don't interfere with code detection. Before this fix, lines like# ) # noqa: ERA001would not be flagged as commented-out code because the# noqa: ERA001part would prevent the POSITIVE_CASES regex from matching and theparse_modulecall from succeeding. Now, after stripping the tool directive, correctly matches the POSITIVE_CASES bracket pattern, and ERA001 fires (and is properly suppressed by the noqa directive).-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.