Skip to content

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Sep 23, 2025

Fixes #24009

Why was the ticket worth tackling?

Lints are inherently imprecise (the "unused" lint, in particular, runs after typer and merely infers usages), so that it is necessary to provide mechanisms to "suppress" them, namely -Wconf and @nowarn. Even if a lint warning is correct, it may not be helpful. For example, the user may want to retain certain unused imports so that they are always available (Decorators) and so that the file "header" isn't subject to repeated edits or reformatting. (See -Yimports.)

But it's also important to address warnings, so that -rewrite might be enabled "by default" and not only for one-off migrations for which manual edits are expected.

Therefore it should be possible to selectively apply rewrites proposed by linters.

Otherwise, linting and rewriting are much less useful.

How I fixed it

It is natural not to rewrite lints that are suppressed. Only "unused imports" emits "code actions" under -rewrite, so the lint was improved to avoid adding actions if ctx.run.suppressions (for @nowarn) or -Wconf say that the warning won't be reported. That mechanism is imprecise, so this filter is moved directly to the reporter (when it reports), which applies actions for LintWarning only.

CheckUnused is tweaked to emit only LintWarning, with the additional tweak that an origin can be a distinguished value to mean NoOrigin. (The origin is just a string, alas, subject to an arbitrary regex pattern match.) It is also tweaked to always calculate the code action. As suggested by a previous reviewer (@tgodzik I presume), this makes the action available to tooling. (The imports rewrite was conceived as a quick internal tool and not as a general code formatter. Used interactively, that may be more convenient, but I haven't tried it.)

Why is this PR worth reviewing?

Certain components emit rewrites directly; for example, the parser might test certain conditions (such as migration), warn about deprecated syntax, and then add a rewrite to contemporary syntax. This PR offers a path toward a more general mechanism: emit a diagnostic with an associated action, and let the user control whether to see the diagnostic and therefore whether to apply the action under -rewrite.

This PR turns out to be more complex in UX than in code.

What's the worse that could happen?

The import action code in CheckUnused is no longer gated by -rewrite, which may be acceptable.

@som-snytt som-snytt force-pushed the issue/24009-rewrite-import-wconf branch from 5706106 to 8c198cf Compare September 26, 2025 22:04
Partially duplicates reporter logic; reporter could auto-apply
actions, maybe keying off diagnostic ID.
Instead of testing whether to emit actions by probing suppressions,
just let the reporter apply LintWarning actions when the warning
is actually reported.
@som-snytt som-snytt force-pushed the issue/24009-rewrite-import-wconf branch from 8c198cf to a7a8f7a Compare October 4, 2025 00:33
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.

Import rewrite should respect -Wconf (rewrite when warning)
1 participant