astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.78k stars 1.07k forks source link

Suppress RUF100 (unused noa) if we see a PGH004 (blanket noqa) error? #8601

Open alanhdu opened 11 months ago

alanhdu commented 11 months ago

This is potentially a very niche request, but I was wondering if there was a way to suppress RUF100 errors for blanket noqa when PGH004 is enabled (and external lints are on)?

Right now, we live in a monorepo with flake8 configured along with our particular sub-codebase's ruff linting. The monorepo wide linting also comes with some company-specific error codes that we use the external setting for. Unfortunately, we're hitting a problem with blanket noqas -- things like:

some_code_that_triggers_internal_lint # noqa

which triggers RUF100 when the real problem is that it should be annotated with # noqa: <internal>. I was hoping to use PGH004 instead to flag these blanket noqa, but am running into two problems:

I think the two things that'd help us with the ruff roll out are to:

Would that make sense? Not sure if this is a reasonable thing to support our if our setup is too idiosyncratic.

charliermarsh commented 11 months ago

Yeah I totally see the issue here. I think the second suggestion is going to be challenging, since we really prefer to keep rule behavior independent of the set of enabled rules (that is, we really want rules to operate independently, such that enabling or disabling one rule shouldn't affect the behavior of another). Breaking that principle can lead to confusing interactions, e.g., if you run ruff check with RULE in your pyproject.toml vs. ruff check --select RULE, you could then see different diagnostics for RULE if it relies on enabled codes. (I should admit that RUF100 is the one rule that actually does suffer from this problem, since it can only detect whether a # noqa is used for the enabled rules, but it at least tells you which rules are known-but-disabled codes.)

A few options, which may all be bad, I'm not sure:

  1. Change the RUF100 to be an unsafe-fix for blanket noqa if external is specified (as proposed above -- this seems reasonable).
  2. Don't flag RUF100 on blank # noqa at all if external is enabled.
  3. If external is enabled, consider making the RUF100 fix such that we add the external codes to a blanket # noqa? I don't know how long external gets in practice.