Sub topic 9
Suppressing violations with noqa comments
This chapter assumes you can run ruff check, read rule codes such as F401, and edit pyproject.toml. Sometimes a rule is right in general but wrong for one line. A # noqa comment lets you silence it there, and leaves the rule on everywhere else.
How noqa works
noqa stands for "no quality assurance". Ruff reads the comment at the end of a line and skips the violations it names on that line. It follows the same convention as Flake8, so existing comments keep working after a migration.
There are two forms:
import os # noqa: F401
value = compute() # noqaThe first form names one rule. Only F401 is silenced on that line. The second form is a bare # noqa, which silences every rule on the line, including ones that appear later when you edit the code. Prefer the targeted form.
You can list several codes, separated by commas:
from helpers import * # noqa: F403, F401A worked example
Take this file, which triggers three violations:
import os
import sys
def total(items):
unused = 1
result = 0
for i in items:
result += i
if result == None:
return 0
return result
print(sys.argv)ruff check reports F401 for os, F841 for unused, and E711 for == None. Suppose os is imported on purpose, because another module reads this_module.os. Add a targeted comment:
import os # noqa: F401Run ruff check again. The F401 report disappears, while F841 and E711 remain. They are real problems, so fix them instead of suppressing them.
Where the comment goes
The comment must sit on the line where Ruff reports the violation. This matters for statements that span several lines.
- For most violations, put the comment at the end of the reported line.
- For a multi-line string, such as a docstring, put the comment on the closing line of the string.
- To skip a whole file, put
# ruff: noqaon its own line. Use# ruff: noqa: F401to skip only one rule for the file.
Finding useless suppressions
Comments go stale. A fix may remove the violation, but the noqa stays behind and hides future problems. Ruff can flag these with rule RUF100 (unused noqa directive). Enable it in your configuration:
[tool.ruff.lint]
extend-select = ["RUF100"]ruff check --fix can also remove unused noqa comments when RUF100 is enabled. Check the diff before committing.
Common errors
- Wrong code.
# noqa: F40does not matchF401. Copy the code from theruff checkoutput. - Missing colon.
# noqa F401has no colon, so Ruff does not read it as a code list and warns about the malformed comment. Write# noqa: F401. - Comment on the wrong line. If the violation is on line 3 of a multi-line call, a comment on line 1 does nothing.
- Bare noqa habit. It hides unrelated rules, so it is the first thing to remove during cleanup.
Choosing the right tool
noqa fits a one-off exception. If a rule fires for a whole kind of file, such as unused imports in __init__.py, a per-file-ignores entry in pyproject.toml is cleaner than a comment on every line. If you disagree with a rule everywhere, remove it from select or add it to ignore. Later chapters cover both.
Exercise
A line reads result = run() # noqa. A teammate later adds a call on the same line that triggers a different rule. Why is this a problem, and how would you rewrite the comment? Which rule helps you find comments like this?
Show answer
A bare # noqa silences every rule on the line, so the new violation is hidden without anyone deciding to hide it. Rewrite it with the specific code, for example # noqa: F841. Enabling RUF100 reports noqa directives that no longer suppress anything, which helps find stale ones.
Exercise
Your package has 12 __init__.py files that each re-export names and trigger F401. Should you add # noqa: F401 to every import line? What do you do instead?
Show answer
No. Add a per-file ignore in pyproject.toml: under [tool.ruff.lint.per-file-ignores], set "__init__.py" = ["F401"]. This covers all those files in one place.