Skip to content

Add --add-ignore for adding ruff:ignore comments#26346

Draft
ntBre wants to merge 1 commit into
mainfrom
brent/add-ignore
Draft

Add --add-ignore for adding ruff:ignore comments#26346
ntBre wants to merge 1 commit into
mainfrom
brent/add-ignore

Conversation

@ntBre

@ntBre ntBre commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds the --add-ignore flag as an alias to --add-noqa. In preview, this adds
ruff:ignore comments with human-readable names instead of noqa comments with codes.

I initially drafted this PR assuming that --add-ignore would be a separate flag that only
functioned in preview mode, while --add-noqa would be available in both preview and stable, but
after talking with Micha realized that we probably want to go ahead and push preview users toward
--add-ignore entirely rather than giving them the choice. I think the one downside of the alias
approach is that --add-noqa still exists in preview but will still add ruff:ignore comments.

In cases where an existing noqa or ruff:ignore comment is present, I opted to preserve the
existing comment and to append the new comment. So something like this:

import math  # noqa: RUF100

becomes:

import math  # noqa: RUF100 # ruff:ignore[unused-import]

instead of also converting the existing noqa comment. I think we can defer to one of our planned
lint rules for the conversion (and joining) instead.

As also shown in this example, the flag adds human-readable names rather than rule codes and prefers
the same placement as a noqa comment, at the end of the line. The one exception to this is an
existing ruff:ignore comment on its own line. In that case, we will reuse and extend the existing
comment instead of adding an additional trailing comment, which I think is a pretty nice balance.

This PR also fixes #26287, which I discovered while working on this and turned out to be easy to fix
while I was here.

Test Plan

Many new CLI tests exercising the various cases Codex and I could come up with. As the server test
shows, this actually applies to the LSP as well, where the Disable for this line action now also
inserts a ruff:ignore comment with a rule name instead of a noqa comment.

@ntBre ntBre added cli Related to the command-line interface preview Related to preview mode features labels Jun 24, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre

ntBre commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

As part of this PR, I'm checking how well --add-noqa works on our ecosystem projects, first running check --select ALL to collect a baseline, then adding noqa comments, and then checking again. One immediate thing I notice is a huge number of added I001 diagnostics (5278 on all ecosystem projects). These aren't particularly alarming, as they seem to be a result of import lines becoming too long with the new comment.

Then there are 923 new RUF100 diagnostics from a variety of rules that seem not to be suppressible. One example is RUF101: https://play.ruff.rs/45d0e92f-fe87-437e-af87-9a83b099ff5b. Another is PLR2044: https://play.ruff.rs/9bcf9da7-4a50-494b-b6d0-cda53188b876. Another is SIM109: https://play.ruff.rs/922b0cf5-b97e-4c9f-8bdc-e300283f35fe.

We can also introduce syntax errors, mostly via UP031 from what I've seen so far:

cat try.py
print("A long string \  # noqa: UP031
   with a continuation on line %d" % 2)python try.py
  File "try.py", line 1
    print("A long string \  # noqa: UP031
          ^
SyntaxError: unterminated string literal (detected at line 1)

There are only 10 of these, but we can also disrupt (our detection of?) script metadata and trigger INP001:

# /// script  # noqa: CPY001
# requires-python = ">=3.11"
# dependencies = [
#     "uv==0.11.13",

I'll plan to open a separate issue or issues for these, but for now I'll just try to make sure --add-ignore doesn't make the situation any worse, at least excluding the isort rules, since the longer ruff:ignore[rule-name] will obviously exacerbate that.

Summary
--

This PR adds the `--add-ignore` flag as an alias to `--add-noqa`. In preview, this adds
`ruff:ignore` comments with human-readable names instead of `noqa` comments with codes.

I initially drafted this PR assuming that `--add-ignore` would be a separate flag that only
functioned in preview mode, while `--add-noqa` would be available in both preview and stable, but
after talking with Micha realized that we probably want to go ahead and push preview users toward
`--add-ignore` entirely rather than giving them the choice. I think the one downside of the alias
approach is that `--add-noqa` still exists in preview but will still add `ruff:ignore` comments.

In cases where an existing `noqa` or `ruff:ignore` comment is present, I opted to preserve the
existing comment and to append the new comment. So something like this:

```py
import math  # noqa: RUF100
```

becomes:

```py
import math  # noqa: RUF100 # ruff:ignore[unused-import]
```

instead of also converting the existing `noqa` comment. I think we can defer to one of our planned
lint rules for the conversion (and joining) instead.

As also shown in this example, the flag adds human-readable names rather than rule codes and prefers
the same placement as a `noqa` comment, at the end of the line. The one exception to this is an
existing `ruff:ignore` comment on its own line. In that case, we will reuse and extend the existing
comment instead of adding an additional trailing comment, which I think is a pretty nice balance.

This PR also fixes #26287, which I discovered while working on this and turned out to be easy to fix
while I was here.

Test Plan
--

Many new CLI tests exercising the various cases Codex and I could come up with. As the server test
shows, this actually applies to the LSP as well, where the `Disable for this line` action now also
inserts a `ruff:ignore` comment with a rule name instead of a `noqa` comment.
@ntBre ntBre force-pushed the brent/add-ignore branch from c355300 to 3b9f7c7 Compare June 25, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli Related to the command-line interface preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--add-noqa deletes trailing content

1 participant