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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45346575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:56:34  来源:igfitidea点击:

What does '# noqa' mean in Python comments?

pythoncommentsterminologycode-analysispep8

提问by Ishpreet

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

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

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

What does noqamean in Python? Is it specific to Python only?

noqa在 Python中是什么意思?它仅特定于 Python 吗?

回答by jimf

Adding # noqato 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.

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

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

这条线可能有一些对 linter 来说“看起来很糟糕”的东西,但开发人员理解并打算出于某种原因将它放在那里。

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

有关更多信息,请参阅有关 Selecting and Ignoring ViolationsFlake8 文档

回答by Vishvajit Pathak

noqa = NO-QA (NO Quality Assurance)

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

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

通常在 Python Programming 中提到忽略PEP8警告。

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

简而言之,以 #noqa 结尾的行将被 linter 程序忽略,并且不会引发任何警告。

回答by YaOzI

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

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

A bit Etymologyof # noqa:

有点词源# 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.

它曾经是“nopep8”,但是当 Flake8 和 Pep8 想要一个共同的限定符时,@florentx 建议使用“NoQA”作为“No Quality Assurance”(iirc)并且它卡住了。

Some basic usagesof # noqa(with flake8):

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

  • # flake8: noqa: filesthat contain this lineare skipped
  • linesthat contain a # noqacomment at the end: will not issue warnings
  • # noqa: <error>, e.g., # noqa: E234at the end: ignore specific errorson a line
    • multiple error codes can be given, separated by comma
    • the colon before the list of codes is required
  • # flake8: noqa:跳过包含这一行的文件
  • 最后包含# noqa注释的:不会发出警告
  • # noqa: <error>,例如,# noqa: E234最后:忽略一行中的 特定错误
    • 可以给出多个错误代码,以逗号分隔
    • 代码列表前的冒号是必需的