Flake8 Noqa Comment Validation
flake8-noqa is a plugin for Flake8 that validates `# noqa` comments in Python code. It ensures that these comments are correctly formatted, are not redundant (i.e., they suppress an actual reported error), and that any specified error codes in `# noqa:` comments correspond to violations found on that line. The current version is 1.5.0, and it's released as needed in conjunction with Flake8 updates.
Warnings
- gotcha Incorrect formatting of `# noqa` comments can lead to them being ignored entirely or interpreted as a blanket `# noqa` (ignoring all errors on the line) even if specific codes were intended. Common mistakes include `# noqa F841`, `# noqa : F841`, or extra spaces.
- gotcha Errors reported by `flake8-noqa` (e.g., about malformed or unnecessary `# noqa` comments) cannot be suppressed using another `# noqa` comment. Trying to do so will not work.
- gotcha Some other Flake8 plugins might perform their own processing of `# noqa` comments and stop reporting violations to Flake8. This can prevent `flake8-noqa` from accurately detecting whether a `# noqa` comment is necessary or correctly matches an existing violation.
- gotcha Using file-level `# flake8: noqa: CODE` (e.g., `# flake8: noqa: E712`) is often misunderstood. Flake8 will ignore *all* errors in the file, not just the specified ones, leading to potential overlooked issues.
Install
-
pip install flake8-noqa
Quickstart
import os
def unused_function(): # noqa: F841
pass
def another_unused_function(): # noqa F842
# This noqa is intentionally malformed to trigger flake8-noqa
pass
def missing_docstring():
# This will trigger a D103 error (if pydocstyle is enabled)
pass