Python 如何禁用特定文件中的 pep8 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18444840/
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 10:46:35  来源:igfitidea点击:

How to disable a pep8 error in a specific file?

pythonpep8

提问by Flows

I tried with

我试过

#:PEP8 -E223

or

或者

# pep8: disable=E223

I thought the second would work but doesn't seems to work.

我认为第二个会起作用,但似乎不起作用。

Do you have an idea how I can handle this ?

你知道我该如何处理吗?

采纳答案by Flows

As far as I know, you can't. You can disable errors or warnings user wide, or per project. See the documentation.

据我所知,你不能。您可以在用户范围内或每个项目中禁用错误​​或警告。请参阅文档

Instead, you can use the # noqacomment at the end of a line, to skip that particular line (see patch 136). Of course, that would skip all PEP8 errors.

相反,您可以使用# noqa行尾的注释来跳过该特定行(请参阅补丁 136)。当然,这将跳过所有 PEP8 错误。

The main author argues against source file noise, so they suggested # pep8comments don't get included.

主要作者反对源文件噪音,所以他们建议# pep8不要包含评论。



Note that there is also nopep8, which is the equivalent. noqa(which stands for No Quality Assurancewas added in version 1.4.1to support people running pyflakesnext to pep8.

请注意,还有nopep8,它是等价的。noqa(代表没有质量保证加入1.4.1版本,支持的人pyflakes旁边pep8

回答by Alejandro

Try putting # nopep8at the end of the line (after two spaces). So if the line of code is:

尝试放在# nopep8行尾(在两个空格之后)。所以如果代码行是:

h=1+2+3+4+5+6+func( "hello","world")

then to ignore the copious pep8 errors for that line it becomes:

然后忽略该行的大量 pep8 错误,它变为:

h=1+2+3+4+5+6+func( "hello","world")  # nopep8

回答by Shrey

You can use --ignoreflag to disable the error you mentioned above

您可以使用--ignore标志来禁用上面提到的错误

pep8 --ignore=E223 file_name.py

for multiple errors

对于多个错误

pep8 --ignore=E223,E501 file_name.py

For more in depth knowledge of other flags you can scan through http://pep8.readthedocs.org/en/latest/intro.html

有关其他标志的更深入了解,您可以浏览http://pep8.readthedocs.org/en/latest/intro.html

回答by Federico

You can do that using Flake8 together with https://github.com/jayvdb/flake8-putty

您可以将 Flake8 与https://github.com/jayvdb/flake8-putty一起使用

回答by Alessandro Cosentino

Let me add something that was probably introduced after all the previous answers were posted.

让我添加一些可能在发布所有先前答案之后引入的内容。

If you use Flake8, you can ignore a specific violation raised in a specific line, by adding

如果您使用 Flake8,您可以忽略特定行中引发的特定违规,方法是添加

# noqa: F401

at the end of the line, where F401here is an example of an error code. For a list of all violations code, see http://flake8.pycqa.org/en/3.5.0/user/error-codes.htmland https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes

在行尾,F401这里是错误代码的示例。有关所有违规代码的列表,请参阅http://flake8.pycqa.org/en/3.5.0/user/error-codes.htmlhttps://pycodestyle.readthedocs.io/en/latest/intro.html #错误代码

You can also ignore all violations in an entire file by adding

您还可以通过添加忽略整个文件中的所有违规

# flake8: noqa

anywhere in the file.

文件中的任何位置。

Reference: http://flake8.pycqa.org/en/3.5.0/user/violations.html

参考:http: //flake8.pycqa.org/en/3.5.0/user/violations.html

回答by Eugene Yarmash

If you use Flake8 3.7.0+, you can ignore specific warnings for entire files using the --per-file-ignoresoption.

如果您使用 Flake8 3.7.0+,您可以使用该--per-file-ignores选项忽略整个文件的特定警告。

Command-line usage:

命令行用法:

flake8 --per-file-ignores='project/__init__.py:F401,F403 setup.py:E121'

This can also be specified in a config file:

这也可以在配置文件中指定:

[flake8]
per-file-ignores =
    __init__.py: F401,F403
    setup.py: E121
    other/*: W9

回答by Gera Zenobi

You can do that with, for example, your setup configuration file (setup.cfg):

例如,您可以使用设置配置文件 ( setup.cfg) 执行此操作:

[tool:pytest]
pep8ignore =
    *.py E501 W503
    api.py E402                <=============== HERE
    doc/* ALL
pep8maxlinelength = 120
flakes-ignore =
    UnusedImport
filterwarnings =
  ignore::DeprecationWarning