What does '# noqa' mean in Python comments?

PythonCommentsTerminologyCode AnalysisPep8

Python Problem Overview


While searching through a Python project, I found a few lines commented with # noqa.

import sys
sys.path.append(r'C:\dev')
import some_module   # noqa

What does noqa mean in Python? Is it specific to Python only?

Python Solutions


Solution 1 - Python

Adding # noqa to a line indicates that the linter (a program that automatically checks code quality) should not check this line. Any warnings that code may have generated will be ignored.

That line may have something that "looks bad" to the linter, but the developer understands and intends it to be there for some reason.

For more information, see the Flake8 documentation for Selecting and Ignoring Violations.

Solution 2 - Python

noqa = NO-QA (NO Quality Assurance)

It's generally referred in Python Programming to ignore the PEP8 warnings.

In simple words, lines having #noqa at the end will be ignored by the linter programs and they won't raise any warnings.

Solution 3 - Python

You know what? Even Guido van Rossum (the creator of Python) asked this question before :D

A bit Etymology of # noqa:

> It used to be "nopep8" but when Flake8 and Pep8 wanted a common > qualifier @florentx suggested "NoQA" as in "No Quality Assurance" > (iirc) and it stuck.

Some basic usages of # noqa (with flake8):

  • # flake8: noqa: files that contain this line are skipped

  • lines that contain a # noqa comment at the end: will not issue warnings

  • # noqa: <error>, e.g., # noqa: E234 at the end: ignore specific errors on a line

    • multiple error codes can be given, separated by comma
    • the colon before the list of codes is required

Solution 4 - Python

Came here after finding a # noqa directive in a library that I was working with. Having never heard of it, I naturally arrived here after searching on Google. The answers provided here are adequate but I wanted to provide some further elaboration for those that may be curious (I certainly was)

  • # noqa has evolved from the # nopep8 syntax used in previous releases of flake8

  • # noqa is supported by IDEs, like PyCharm, for use with their built-in code inspection tools.

  • # noqa can be used as a pre-commit directive, such that prior to new commits an inspection process must complete

  • # noqa can be used to ignore all warnings or given specific warnings to ignore. For example, # noqa: F401 will ignore an unused imported module warning.

As an example, consider the following code:

import os

print("Hello, world!")

This code imports the os module but doesn't use it. If one wanted to use the # noqa tool to suppress a PEP8 warning, it could be written as such:

import os  # noqa

print("Hello, world!")

This will ignore all warnings. However, if one were only to want to ignore a specific warning (PEP8 F401 imported but not used), it could be done as such:

import os  # noqa: F401

print("Hello, world!")

I've published an article with some noqa examples and more elaboration on the above points.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIshpreetView Question on Stackoverflow
Solution 1 - PythonjimfView Answer on Stackoverflow
Solution 2 - PythonVishvajit PathakView Answer on Stackoverflow
Solution 3 - PythonYaOzIView Answer on Stackoverflow
Solution 4 - PythonalphazwestView Answer on Stackoverflow