Two ways a suppression goes stale#
A suppression comment records a decision about one finding on one line. Code keeps changing after the decision, and the comment can drift in two directions. The original finding can disappear while the comment stays, which is harmless but misleading. Or a different finding can arrive on the same line, and a comment that names no code silences it without anyone deciding anything. The first kind is visible to the tool's unused-suppression check. The second kind is invisible to that check, because the suppression is still in use.
Both linters below have a check for each direction. The examples are constructed and were run with ruff 0.17.6 and mypy 2.4.1 on Python 3.11.
mypy: a bare ignore that absorbed a new error#
The first version passes a value typed object to a function that expects str, and someone silenced the argument error:
# jobs.py, version 1
def parse_seconds(raw: str) -> float:
return float(raw)
def timeout_ms(settings: dict[str, object]) -> float:
return parse_seconds(settings["timeout"]) * 1000 # type: ignore
Later, parse_seconds learned to return None for an empty setting. The last line did not change:
# jobs.py, version 2
def parse_seconds(raw: str) -> float | None:
if not raw:
return None
return float(raw)
mypy reports nothing for either version. Without the comment, version 2 has two errors on the line, and the second one is new:
$ mypy jobs.py # version 2 with the comment removed
jobs.py:8: error: Unsupported operand types for * ("None" and "int") [operator]
jobs.py:8: note: Left operand is of type "float | None"
jobs.py:8: error: Argument 1 to "parse_seconds" has incompatible type "object"; expected "str" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
The new error is a real bug. With an empty timeout setting, the function raises at runtime:
$ python -c "import jobs; print(jobs.timeout_ms({'timeout': ''}))"
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
The comment hid that error the moment it appeared. mypy's error code documentation gives the reason to name codes: "It's possible to silence specific error codes on a line using # type: ignore[code]. This way you won't accidentally ignore other, potentially more serious errors." Had the original author written # type: ignore[arg-type], version 2 would have reported the new error and said why it was not covered:
$ mypy jobs.py # version 2 with "# type: ignore[arg-type]"
jobs.py:8: error: Unsupported operand types for * ("None" and "int") [operator]
jobs.py:8: note: Error code "operator" not covered by "type: ignore[arg-type]" comment
jobs.py:8: note: Left operand is of type "float | None"
Found 1 error in 1 file (checked 1 source file)
Ask mypy what each suppression is hiding#
You can audit existing bare ignores without deleting them one at a time. The optional error code ignore-without-code flags every bare # type: ignore, and its message lists the codes the comment is silencing right now:
$ mypy --enable-error-code ignore-without-code jobs.py # version 1
jobs.py:6: error: "type: ignore" comment without error code (consider "type: ignore[arg-type]" instead) [ignore-without-code]
Found 1 error in 1 file (checked 1 source file)
$ mypy --enable-error-code ignore-without-code jobs.py # version 2
jobs.py:8: error: "type: ignore" comment without error code (consider "type: ignore[arg-type, operator]" instead) [ignore-without-code]
Found 1 error in 1 file (checked 1 source file)
Compare the suggestion with the reason the comment was written. Version 1 suggests one code; version 2 suggests two, and operator is the new one. The optional error code list states the purpose: it "clarifies the intent of the ignore and ensures that only the expected errors are silenced." Set enable_error_code = ignore-without-code in the mypy configuration so new bare ignores fail review, then replace each one with the codes it was meant for.
The other direction is the original error going away. A third version, starting again from version 1 with the comment narrowed to # type: ignore[arg-type], converts the value with str(), which fixes the argument error and leaves the named ignore with nothing to do. --warn-unused-ignores reports it:
$ mypy --warn-unused-ignores jobs.py # version 3
jobs.py:6: error: Unused "type: ignore" comment [unused-ignore]
Found 1 error in 1 file (checked 1 source file)
The command-line reference describes the flag: it "will make mypy report an error whenever your code uses a # type: ignore comment on a line that is not actually generating an error message." Delete such a comment. Leaving it in place turns it into a bare suppression waiting for the next error on that line.
ruff: RUF100 reports unused codes, not hidden ones#
Ruff's RUF100 is the counterpart of --warn-unused-ignores. The ruff linter documentation at 0.17.6 says it checks that "the violations they say they ignore are actually being triggered and suppressed." This constructed file has one stale suppression and one blanket one:
import json # noqa: F401
import subprocess
def manifest(tag):
return json.dumps({"image": tag})
def push(tag):
return subprocess.call(f"docker push registry.example.com/web:{tag}", shell=True) # noqa
$ ruff check --output-format concise --select F,S,RUF100 deploy.py
deploy.py:1:14: RUF100 [*] Unused `noqa` directive (unused: `F401`)
Found 1 error.
[*] 1 fixable with `--fix`.
json is now used, so the F401 suppression on line 1 is stale and RUF100 says so. Line 10 passes. Its bare # noqa is in use, so RUF100 has nothing to report, and the output gives no hint of what it suppresses. --ignore-noqa shows it:
$ ruff check --output-format concise --select F,S,RUF100 --ignore-noqa deploy.py
deploy.py:1:14: RUF100 [*] Unused `noqa` directive (unused: `F401`)
deploy.py:10:12: S602 `subprocess` call with `shell=True` identified, security issue
Found 2 errors.
[*] 1 fixable with `--fix`.
A shell=True call with an interpolated tag is the kind of finding a reviewer should see. The rule that forbids blanket comments is PGH004, which the rule's source explains: "Suppressing all diagnostics can hide issues in the code."
$ ruff check --output-format concise --select F,S,RUF100,PGH004 deploy.py
deploy.py:1:14: RUF100 [*] Unused `noqa` directive (unused: `F401`)
deploy.py:10:88: PGH004 Use specific rule codes when using `noqa`
Found 2 errors.
[*] 1 fixable with `--fix`.
The same reasoning applies to other tools' suppression comments: a comment with a rule identifier can be checked against what the tool still reports; one without can only be checked by running the tool with suppressions turned off.
When the suppression is still correct#
Running with suppressions off can also confirm the comment. If --ignore-noqa or the ignore-without-code message shows exactly the code the comment names, and the reason recorded beside it still holds (an intentional re-export, a third-party API typed loosely), the suppression is doing its job. Keep it, and add the code if it was bare. The finding under it was decided once; what you are checking is that nothing else joined it.
Decide from what the suppression covers#
| What you see | Evidence to collect | Decision |
|---|---|---|
RUF100 or unused-ignore on a comment | The tool's report that nothing on the line triggers | Delete the comment |
Bare # type: ignore near a finding | ignore-without-code message listing current codes | Replace with the intended code; fix any code that was not intended |
Bare # noqa near a finding | ruff check --ignore-noqa on the file | Same: name the intended rule, fix the rest |
| Named suppression, new finding on the same line | The new code in the output ("not covered by") | Treat the new finding on its merits |
| Named suppression, tool shows only that code | The reason recorded with the comment | Keep it |
When the suppressed finding came from an automated reviewer and was dismissed in a review thread, the guide to reconstructing a dismissed finding covers how to recover why. When mypy's error comes from a stub that disagrees with the library, suppressing it is the wrong repair; the guide to type-checker errors from the wrong stub shows how to tell.