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
I'll start by exploring the repository structure to understand the codebase and then reproduce the issue.
Assistant
I need to make only one tool call at a time. Let me start by exploring the repository structure.
Assistant
This is the ruff repository. Let me find the ERA001 rule implementation.
Assistant
Now let me reproduce the issue. Let me first create a test file with the problematic content.
Assistant
Let me run ruff on this file to confirm the issue.
Assistant
I've confirmed the issue. Now let me look at the ERA001 rule implementation to understand how comments are processed.
Assistant
Now let me understand how the noqa directive is parsed. Let me search for the noqa handling code.
Assistant
Let me look at where
comment_contains_codeis used.Assistant
Let me look at the noqa directive lexing to understand how noqa directives are detected within lines.
Assistant
Let me look at the
lex_inline_noqafunction and related lexer code.Assistant
This is getting complex. The noqa lexer handles multiple comments in a single comment range. So
# comment # noqa: F401would be handled.Let me think about the best approach for the fix.
The core issue is: ERA001 analyzes the full comment text including the
# noqa: ...directive, which changes whether the comment is detected as code.The most robust fix is to strip the trailing noqa directive from the comment before analyzing it. This way, becomes which is then correctly detected as code, and the noqa directive suppresses the warning.
Assistant
Let me look at how the noqa directive range is found and whether there's a helper to find/strip the noqa portion. Let me check the
is_noqa_uncasedand search for noqa-stripping helpers.Assistant
Let me see the rest of the noqa lexer to understand how codes are parsed and where the directive ends.
Assistant
Let me look at the existing test fixtures for eradicate to understand existing test cases, especially around noqa.
Assistant
Let me look at the test for ERA001 and the snapshot file to understand expected behavior.
Assistant
Let me look at how
NoqaDirectives::from_commented_rangesworks and how RUF100 detects noqa directives.Assistant
Let me look at how noqa directives are detected elsewhere, perhaps there's a regex pattern I can reuse.
Assistant
There's a
find_trailing_pragma_offsetfunction. Let me examine it.Assistant
This
find_trailing_pragma_offsetfunction is exactly what I need. Let me verify it's exported and check its usage.Assistant
Let me look at how
overlong.rsuses these functions to understand the pattern.Assistant
Let me look at the full ERA001.py fixture to check for any pragmas I might affect.
Assistant
Now I understand the issue thoroughly. The problem is in
comment_contains_code: when a comment has a trailing pragma like# noqa: ERA001, the pragma text is included in the code analysis, which can cause a comment that would be detected as code to no longer be detected as code. This leaves the noqa directive unused (RUF100) while the same comment without the directive is flagged (ERA001) — the "Schrödinger's comment" problem.There's already a
find_trailing_pragma_offsetfunction inruff_python_triviathat finds the offset where a trailing pragma comment starts. I'll use it to strip the trailing pragma before code analysis.Let me make the edit:
Assistant
Let me view the exact function definition lines to edit.
Assistant
Now let me make the edits. First, update the import:
Assistant
The import is updated. Now let me update the
comment_contains_codefunction to strip the trailing pragma:Assistant
Now let me build and run the unit tests for the eradicate detection module.
Assistant
All unit tests pass. Now let me run the ERA001 fixture test to ensure the snapshot still matches:
Assistant
The fixture test passes (snapshot unchanged). Now let me rebuild the ruff binary and verify the issue is fixed:
Assistant
The issue is fixed. Now line 1 reports ERA001, and line 2's
# noqa: ERA001is correctly used to suppress ERA001 (so no RUF100). Only 1 error remains.Let me test edge cases to ensure robustness:
Assistant
Let me analyze these edge cases:
- Lines 1, 2, 5, 7: code with
# noqa: ERA001or# noqa→ ERA001 fires but is suppressed by the noqa. No diagnostic. ✓ - Line 3
# ): ERA001 fires (no noqa). ✓ - Line 4
# ) # noqa: F401: ERA001 fires (the)is code), and the noqa is for F401 (non-enabled), so RUF100 reports non-enabled F401. This is correct — the)is commented-out code and the F401 noqa doesn't suppress ERA001. - Line 6
# This is a (real) comment. # noqa: ERA001: Not code (consecutive identifiers "This is a"), so noqa is unused → RUF100. Correct and pre-existing behavior.
Let me run the broader RUF100 and related tests to ensure nothing broke:
- Lines 1, 2, 5, 7: code with
Assistant
All RUF tests pass. Let me specifically check the RUF100 tests and the noqa-related tests:
Assistant
Let me also run the noqa and pycodestyle (overlong) tests since they share the pragma logic:
Assistant
All tests pass. Let me verify the exact issue reproduction once more, including the leading tabs from the issue:
Assistant
The issue is fully resolved. Let me run clippy on the changed module to ensure code quality:
Assistant
Clippy is clean. Let me run the formatter tests as well, since the formatter also uses the pragma function (to be thorough even though I didn't modify it):
Assistant
All tests pass. Let me review the final diff:
Assistant
The diff is clean and minimal. Let me do a final comprehensive test using the exact config from the issue:
Assistant
The issue is fully resolved. With the exact config from the issue:
- Before: 2 errors (ERA001 on line 1 + RUF100 on line 2 — the "Schrödinger's comment")
- After: 1 error (ERA001 on line 1 only). Line 2's
# noqa: ERA001now correctly suppresses ERA001, so no RUF100.
Let me clean up the temporary test files:
Assistant
Let me run the eradicate tests one final time to confirm everything is in order before submitting:
Assistant
All tests pass. Let me provide my final summary and submit.
Summary
The Issue ("Schrödinger's comment")
A comment like
# )is detected as commented-out code (ERA001), but the same comment with a trailing directive () is detected as code. This creates a paradox:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.