Python
noqa
comments
code linting
flake8

What does # noqa mean in Python comments (e.g. # noqa: E402)?

Codemia Knowledge Hub ·

# noqa at the end of a Python line tells the linter to skip checking that line. The name stands for "no quality assurance." It originated with Flake8 and is honored by Flake8 (including the pycodestyle and pyflakes checks it runs), pycodestyle, and Ruff. Standalone pyflakes ignores it. Adding a code, as in # noqa: E402, suppresses only that rule.

Treat it as an exception mechanism: fix the code when possible, suppress narrowly when necessary, and document why. Teams that overuse # noqa hide real defects behind blanket suppressions.

How # noqa works

As a linter scans each line for violations, it checks whether the line ends with # noqa before reporting. If it does, the violation is dropped from the output.

The bare form suppresses every rule on the line:

python
import legacy_module  # noqa

That also hides violations you might want to see, like an unused import sitting next to a naming convention warning.

Suppressing specific rules with error codes

List the rule codes after a colon to suppress only those:

python
from package import *  # noqa: F403

Separate multiple codes with commas:

python
from legacy import helper, utils  # noqa: F401, E501

F401 is "imported but unused" and E501 is "line too long." Everything else on the line is still checked.

Common Flake8 and Ruff codes, including E402

CodeRuleWhen to suppress
F401Imported but unusedRe-exports in __init__.py
F403Wildcard import usedLegacy modules requiring * imports
E501Line too longURLs or long string literals that cannot be broken
E402Module-level import not at topImports that must follow sys.path manipulation
W503Line break before binary operatorStyle preference conflicts between tools
E711Comparison to NoneSQLAlchemy filter expressions using == None

When # noqa is justified

Re-exports in __init__.py: When a package assembles its public API by importing names from submodules, the linter sees those imports as unused:

python
1# mypackage/__init__.py
2from .core import Engine  # noqa: F401
3from .config import Settings  # noqa: F401
4from .exceptions import ValidationError  # noqa: F401

Compatibility imports: Libraries supporting multiple Python versions sometimes need conditional imports:

python
1try:
2    from functools import cached_property  # noqa: F401
3except ImportError:
4    from django.utils.functional import cached_property  # noqa: F401

SQLAlchemy filter expressions: ORM filters compare to None with == on purpose, which triggers E711:

python
query = session.query(User).filter(User.deleted_at == None)  # noqa: E711

Generated code: Auto-generated files often break style rules by design, so a file-level suppression may be appropriate.

Suppressing a whole file

Flake8 supports a file-level directive, placed near the top of the file:

python
# flake8: noqa

Ruff has a similar mechanism. Use it for auto-generated files or vendored third-party code, never for files your team maintains: it hides every present and future violation, so defects pile up silently.

Suppression syntax in other tools

If your project runs several tools, each respects only its own comments.

ToolInline suppression syntaxFile-level syntax
Flake8# noqa: F401# flake8: noqa
Ruff# noqa: F401 (same as Flake8)# ruff: noqa
Pylint# pylint: disable=unused-import# pylint: disable-all
Mypy# type: ignore[assignment]Per-file config in mypy.ini
Pyright# pyright: ignore[reportGeneralTypeIssues]Per-file config

# noqa does nothing for Pylint, and # pylint: disable does nothing for Flake8.

Finding stale # noqa comments with Ruff

Ruff has become the dominant Python linter because of its speed. Besides honoring # noqa, ruff check --extend-select RUF100 flags # noqa comments that suppress rules no longer being violated, so you can clean up suppressions left behind by refactors.

bash
# Find unused noqa directives
ruff check --extend-select RUF100 src/

Team rules for suppressions

Require specific codes: Ban bare # noqa in new code. In Ruff, enable RUF100. In Flake8, use the flake8-noqa plugin.

Require a rationale comment:

python
from .plugin_api import register  # noqa: F401 -- public API re-export

Track suppression counts: Audit # noqa occurrences periodically. A rising count without justifications suggests the team is avoiding fixes.

bash
# Count noqa occurrences in a project
grep -r "# noqa" src/ --include="*.py" | wc -l

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms