flake8:仅忽略整个文件中的 F401 规则

AJ *_*end 22 python flake8

有没有办法flake8只忽略整个文件的特定规则?具体来说,我只想忽略F401整个文件。

我有一个文件,比如__init__.py我在其中导入从未在该文件中使用过的符号。我宁愿不添加# noqa到每一行。我可以添加# flake8: noqa到文件的开头,但这会忽略所有规则。我想忽略只是F401规则。

Ant*_*ile 34

there is not currently a way to do what you're asking with only source inside the file itself

the current suggested way is to use the per-file-ignores feature in your flake8 configuration:

[flake8]
per-file-ignores =
    */__init__.py: F401
Run Code Online (Sandbox Code Playgroud)

Note that F401 in particular can be solved in a better way, any names that are exposed in __all__ will be ignored by pyflakes:

[flake8]
per-file-ignores =
    */__init__.py: F401
Run Code Online (Sandbox Code Playgroud)

(disclaimer: I'm the current maintainer of flake8 and one of the maintainers of pyflakes)

  • 我想说这与其说是免责声明,不如说是权威声明:) (3认同)
  • 好的。这有帮助。谢谢!您是否会考虑对位于文件本身顶部的每个文件规范进行功能请求?(如果不是特别是“F401”,那么可能是其他错误。) (3认同)

Jua*_*n C 8

根据文档,只需更改即可简单# noqa

# noqa: F401
Run Code Online (Sandbox Code Playgroud)

  • 是的,我知道这种语法,但我正在寻找一种方法来在文件中表示它,这样我就不必修改命令行字符串。 (10认同)
  • 我认为这只是一行,而不是整个文件。整个文件的正确语法是什么?例如,文件顶部的“# flake8: noqa: F401”仍然忽略**所有**规则。 (6认同)