Skip to content

Preserve trailing content in --add-noqa and --add-ignore#26409

Draft
ntBre wants to merge 2 commits into
mainfrom
brent/preserve-reason
Draft

Preserve trailing content in --add-noqa and --add-ignore#26409
ntBre wants to merge 2 commits into
mainfrom
brent/preserve-reason

Conversation

@ntBre

@ntBre ntBre commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #26287, currently stacked on #26346

Test Plan

New CLI tests

ntBre added 2 commits June 26, 2026 09:36
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.

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.
Summary
--

Fixes #26287, currently stacked on #26346

Test Plan
--

New CLI tests
@astral-sh-bot

astral-sh-bot Bot commented Jun 26, 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 force-pushed the brent/add-ignore branch from ac10c98 to b141602 Compare June 26, 2026 17:49
@ntBre ntBre linked an issue Jun 28, 2026 that may be closed by this pull request
ntBre added a commit that referenced this pull request Jul 7, 2026
Summary
--

This PR adds the new `--add-ignore` flag. In preview, this adds
`ruff:ignore` comments with human-readable names instead of `noqa`
comments with codes. Without preview enabled, using the flag emits an
error. `--add-noqa` itself is still available in both preview and stable
mode.

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.

Test Plan
--

Many new 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.

Ecosystem check
--

I also ran some tests against ecosystem projects as described in my
[comment](#26346 (comment))
below. These revealed some additional discrepancies between
`ruff:ignore` and `noqa` (and some bugs in `noqa`/`--add-noqa` itself),
but I think it makes sense to leave these as follow-ups. I've already
opened (rough) drafts of them in:
- #26373
- #26409
- #26412

We could land the shebang fixes separately, but the other two are
stacked on this PR. With all three of these applied, the ecosystem check
showed parity with `--add-noqa`.

This also seems like an interesting check to add to our ecosystem
analysis in general.
Base automatically changed from brent/add-ignore to main July 7, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--add-noqa deletes trailing content

1 participant