问题:Python注释中的“#noqa”是什么意思?

在搜索Python项目时,我发现有几行用注释# noqa

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

noqa在Python 中是什么意思?它仅适用于Python吗?

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?


回答 0

添加# noqa到一行表示该linter(自动检查代码质量的程序)不应检查该行。代码可能已生成的任何警告都将被忽略。

该行可能对短毛绒“看起来不好”,但由于某种原因,开发人员理解并打算将其放在那里。

有关更多信息,请参见Flake8文档,以选择和忽略违规

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.


回答 1

noqa = NO-QA(无质量保证)

在Python编程中通常引用它来忽略PEP8警告。

简而言之,linter程序将忽略末尾带有#noqa的行,并且不会发出任何警告。

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.


回答 2

你知道吗?甚至Guido van Rossum(Python的创建者):D 之前也问过这个问题。

有点词源# noqa

它曾经是“ nopep8”,但是当Flake8和Pep8想要一个普通的限定词时,@ florentx像“ No Quality Assurance”(iirc)中一样建议“ NoQA”,并且卡住了。

一些基本用法# noqa(与flake8):

  • # flake8: noqa:跳过包含此行的文件
  • 最后包含# noqa评论的:将不会发出警告
  • # noqa: <error>,例如# noqa: E234 最后:忽略一行中的 特定错误
    • 可以给出多个错误代码,以逗号分隔
    • 代码列表之前的冒号

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。