Sub topic 9
Suppressing Violations with noqa Comments
Sometimes a lint rule is right in general but wrong for one specific line. A noqa comment tells Ruff to skip a rule at that spot. This chapter covers the comment syntax, where Ruff looks for it, and how to stop suppressions from piling up unnoticed.
It assumes you can run ruff check and read [tool.ruff.lint] in pyproject.toml. It also assumes you know how rule codes like F401 and prefixes like F work.
Why suppression is a last resort
Every noqa is a promise that a human has judged the rule wrong at that place. If you suppress too widely, real bugs hide behind comments. The usual order of preference is:
- Fix the code.
- Change the configuration if the rule does not fit your project.
- Use
per-file-ignoresif the rule does not fit a group of files. - Use
noqafor a single justified exception.
Line-level noqa
A # noqa comment applies to the line that carries it. The form with a code is the one to use:
import os # noqa: F401
import sys
def compute():
return 42
def is_missing(value):
if value == None: # noqa: E711
return True
return False
value = compute() # noqa
print(sys.argv, is_missing(value))The three comments differ in scope:
import os # noqa: F401silences only F401 (unused import) on that line. Any other violation there is still reported.value == None # noqa: E711silences only the comparison-to-Nonerule.- A bare
# noqasilences every rule on its line, including rules that do not exist yet in your configuration.
The bare form is a trap. If you later add a second problem to that line, Ruff will not report it. Prefer the targeted form.
Listing several codes
Separate codes with commas:
import os, sys # noqa: E401, F401Use full codes such as F401. Do not rely on prefixes like F4 inside a comment. Full codes keep the intent unambiguous and are what Ruff's own tooling writes.
Where Ruff looks for the comment
Ruff finds the directive inside a comment, even after other text. This lets noqa share a comment with a type-checker directive:
import typing_extensions # type: ignore # noqa: F401Keep a short reason next to the code if your team wants one. Text after the codes is generally ignored by the parser, but a comma-separated list followed by prose can be misread, so put the reason before the directive:
import legacy_plugin # registers itself on import # noqa: F401Multi-line constructs
The comment must sit on the line where Ruff reports the violation. The ruff check output shows that line as path:line:column. This matters most for statements that span several lines.
A multi-line string is the main special case. A noqa after the closing quotes covers every line inside the string. This is the usual way to handle E501 (line too long) inside a long SQL string, once you have enabled E501:
QUERY = """
SELECT id, name, email, created_at, updated_at, deleted_at, last_login_at, profile_url
FROM users
""" # noqa: E501
print(QUERY)E501 is not in Ruff's default rule set. The defaults are E4, E7, E9 and F. Select E501 explicitly in pyproject.toml if you want it checked.
For a parenthesized call or import spread over several lines, put the comment on the line named in the diagnostic, not necessarily the first line of the statement.
File-level suppression
A comment that sits alone on its own line and starts with ruff: noqa applies to the whole file:
# ruff: noqa: F401
"""Package facade that re-exports names for users."""
from .models import User
from .views import renderWithout codes, # ruff: noqa disables all linting for that file. Ruff also accepts the Flake8 spelling # flake8: noqa and treats it like the Ruff one, which helps when you inherit older code.
Two details cause confusion:
- The file-level form must be on its own line. Putting
# ruff: noqa: F401at the end of a code line does not make it file-wide. - A line-level
# noqa: F401is a different thing. It never spreads beyond its own line.
Choosing the right mechanism
The diagram shows the order of questions to ask before adding a comment.
| Mechanism | Scope | Lives in | Typical use |
|---|---|---|---|
| `ignore = ["E501"]` | Whole project | `pyproject.toml` | A rule your team has decided against |
| `per-file-ignores` | Files matching a glob | `pyproject.toml` | `F401` in `__init__.py`, asserts in tests |
| `# ruff: noqa: F401` | One file | The file | A generated or re-export module |
| `# noqa: F401` | One line | The line | A single justified exception |
Keeping suppressions honest
Suppressions go stale. The code changes, the violation disappears, and the comment stays. Three tools help.
RUF100: unused noqa
RUF100 reports a noqa directive that suppresses nothing. It is not in the default set, so select it. Its fix removes the dead comment.
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "RUF100", "PGH004"]Run the cleanup on its own:
ruff check --select RUF100 --fix .RUF100 also flags codes in a noqa that no longer match anything on the line, so a list like # noqa: E501, F401 gets trimmed to the codes that still fire.
PGH004: blanket noqa
PGH004 reports a bare # noqa with no codes. Selecting it forces every suppression to name a rule. A project that wants the targeted form everywhere gets that for free.
Codes Ruff does not know
If your comments name codes from another tool, RUF100 would call them unused. List those codes under external, and Ruff will leave them alone:
[tool.ruff.lint]
external = ["V101"]Adding and bypassing noqa in bulk
Turning on a new rule in a large codebase can produce hundreds of violations. --add-noqa writes a # noqa: CODE comment on every line that currently violates a selected rule:
ruff check --select B --add-noqa .This gives you a clean run now and a searchable list of debt to work off later. Review the diff before you commit it, since it touches many files.
To see what the code would report with every suppression ignored, for example to audit them, use:
ruff check --ignore-noqa .Common errors
- A bare
# noqahides new problems. Name the code. - The comment is on the wrong line. The violation line in the output is the one that counts, which is the last line for a multi-line string.
- A typo in the code.
# noqa: F410suppresses nothing, and the real violation still shows up. Ruff can report invalid codes under RUF102 if you select it. - Expecting
# noqato also stop the formatter. It does not. The formatter has its own directives, covered next. - Using
noqafor whatper-file-ignoreshandles. Twenty identical# noqa: F401comments in one package signal that a configuration entry is the better tool.
Formatter directives are separate
noqa only affects ruff check. To keep ruff format away from a region, use the formatter's own comments:
# fmt: off
matrix = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
]
# fmt: on
result = {"a":1} # fmt: skipEverything between # fmt: off and # fmt: on stays as written. # fmt: skip at the end of a statement leaves that statement untouched.
Exercise
A file contains the line import os, sys # noqa. You want Ruff to keep reporting any new problems on that line but still allow the unused imports. What should you write, and which rule would flag the original form if you selected it?
Show answer
Write import os, sys # noqa: E401, F401, which names only the multiple-imports rule (E401) and the unused-import rule (F401). The bare # noqa would be flagged by PGH004 (blanket noqa).
Exercise
Your pyproject.toml selects RUF100. A teammate removes an unused import but leaves # noqa: F401 on the same line, in a case where the line now holds import json and json is used. What does ruff check report, and how do you clear it automatically?
Show answer
It reports RUF100 (unused noqa directive), because nothing on that line violates F401 any more. Run ruff check --select RUF100 --fix . and Ruff removes the comment.
Exercise
A package has an __init__.py that re-exports names, and the same F401 suppression appears in eight such files. Which mechanism fits better than eight noqa comments, and what is the setting?
Show answer
Use per-file-ignores. Add "__init__.py" = ["F401"] under [tool.ruff.lint.per-file-ignores] in pyproject.toml.