Skip to content

[ruff] Fix for RUF102 should delete entire comment#23380

Merged
amyreese merged 3 commits intomainfrom
amy/ruf102-reasons
Feb 17, 2026
Merged

[ruff] Fix for RUF102 should delete entire comment#23380
amyreese merged 3 commits intomainfrom
amy/ruf102-reasons

Conversation

@amyreese
Copy link
Member

Prevents generating invalid syntax for noqa comments with trailing text

Fixes #23349

Prevents generating invalid syntax for noqa comments with trailing text

Fixes #23349
@amyreese amyreese requested review from dylwil3 and ntBre February 17, 2026 18:53
@amyreese amyreese added fixes Related to suggested fixes for violations bug Something isn't working labels Feb 17, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 17, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dylwil3
Copy link
Collaborator

dylwil3 commented Feb 17, 2026

I'm not sure about this... could we delete just the noqa: INVALID123 but not the leading # if there is text after?

At the very least it feels like the fix should be unsafe in this case?

(sort of surprised it isn't always unsafe, to be honest)

@amyreese
Copy link
Member Author

FWIW, this matches the behavior of the RUF100 fix when all codes are unused, which also deletes the entire comment.

@ntBre
Copy link
Contributor

ntBre commented Feb 17, 2026

If I remember correctly, we assume the rest of the text is the "reason" for the noqa, which is why we delete it too. There's a (possibly closed) issue about this around somewhere. #20762 and #12251, for example.

@amyreese
Copy link
Member Author

amyreese commented Feb 17, 2026

Also worth noting that currently this fix is only relevant if the user has RUF100 disabled, because otherwise that rule would also flag this line and offer a fix to remove the entire comment:

RUF100:

amethyst@lunatone ~/workspace/ruff amy/ruf102-reasons » ruff-check --isolated --select RUF100 --diff ../test.py
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/ruff check --no-cache --isolated --select RUF100 --diff ../test.py`
warning: Invalid rule code provided to `# noqa` at /Users/amethyst/workspace/test.py:3: TK412
--- /Users/amethyst/workspace/test.py
+++ /Users/amethyst/workspace/test.py
@@ -1,3 +1,3 @@
-pass  # noqa: E501 some reason
+pass

-pass  # noqa: TK412 some reason
+pass

Would fix 2 errors.
> [1]

RUF102:

amethyst@lunatone ~/workspace/ruff amy/ruf102-reasons » ruff-check --isolated --select RUF102 --diff ../test.py
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.09s
     Running `target/debug/ruff check --no-cache --isolated --select RUF102 --diff ../test.py`
warning: Invalid rule code provided to `# noqa` at /Users/amethyst/workspace/test.py:3: TK412
--- /Users/amethyst/workspace/test.py
+++ /Users/amethyst/workspace/test.py
@@ -1,3 +1,3 @@
 pass  # noqa: E501 some reason

-pass  # noqa: TK412 some reason
+pass

Would fix 1 error.
> [1]

@dylwil3
Copy link
Collaborator

dylwil3 commented Feb 17, 2026

Somehow the situation of RUF102 feels slightly different than that of RUF100. In RUF100, we're pretty confident it's one of our rules being suppressed, and we helpfully see it's not doing anything. In this case we think we've identified some wrong code, but it may be serving some purpose unknown to us.

But I am happy to be overruled!

Maybe we should separately put something like this https://docs.astral.sh/ruff/rules/unused-noqa/#conflict-with-other-linters in the docs for RUF102 as well? (can be a separate PR)

@amyreese
Copy link
Member Author

In RUF100, we're pretty confident it's one of our rules being suppressed, and we helpfully see it's not doing anything.

Take a look at my examples above. RUF100 will fire for a noqa with an "unknown" code, and suggest to fix it by removing the code and/or comment. This is a behavior overlap with RUF102, to be clear.

@dylwil3
Copy link
Collaborator

dylwil3 commented Feb 17, 2026

Hmm... well that seems weird. Should we remove that behavior from RUF100? Sorry this discussion keeps sprawling in scope.

@amyreese
Copy link
Member Author

Also considered that this matches the behavior for how the new range suppressions fix RUF100/102/103 errors, where they also assume anything up to # (the next comment node) is a "reason" and remove it as part of the fix.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This makes sense to me, and it's nice to reuse delete_comment and clean up some of the whitespace issues in the previous fix. Should we add a test case specifically for the syntax error?

We may want to open follow-up issues for some of the other discussion here, but I think this is fine to land to resolve the syntax error.

@amyreese amyreese enabled auto-merge (squash) February 17, 2026 20:46
@amyreese amyreese merged commit aeb45ae into main Feb 17, 2026
41 checks passed
@amyreese amyreese deleted the amy/ruf102-reasons branch February 17, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RUF102 fix causes syntax error by not handling text following directive

3 participants