简体   繁体   English

Flake8:忽略整个文件的特定警告

[英]Flake8: Ignore specific warning for entire file

The Ignoring Errors docs currently list a way of ignoring a particular error for a particular line: 忽略错误文档目前列出了一种忽略特定行的特定错误的方法:

example = lambda: 'example'  # noqa: E731

... and a way of ignoring all errors for an entire file: ...以及忽略整个文件所有错误的方法:

# flake8: noqa

from foo import unused
function_that_doesnt_exist()
x = 1+       2

... and a couple of ways, either through config or through command-line options, of disabling a particular error globally across an entire project. ...以及通过配置或命令行选项在整个项目中全局禁用特定错误的几种方法。

But what if I want to ignore a particular error across the entirety of a single file - for instance, to disable warnings about unused imports in an __init__.py barrel file that just imports a bunch of classes so that code from other packages can import them from it in turn?但是,如果我想忽略整个单个文件中的特定错误怎么办 - 例如,在__init__.py桶文件中禁用有关未使用导入的警告,该文件仅导入一堆类,以便其他包中的代码可以导入它们反过来呢? The docs don't seem to hint at any syntax for this.文档似乎没有暗示任何语法。 Is it possible?是否可以?

As of Flake8 3.7.0 you can do this using the --per-file-ignores option.从 Flake8 3.7.0 开始,您可以使用--per-file-ignores选项执行此操作。

Command line example命令行示例

flake8 --per-file-ignores="project/__init__.py:F401 setup.py:E121"

Or in your config file或者在你的配置文件中

per-file-ignores =
    project/__init__.py:F401
    setup.py:E121
    other_project/*:W9

See the documentation here: http://flake8.pycqa.org/en/latest/user/options.html?highlight=per-file-ignores#cmdoption-flake8-per-file-ignores请参阅此处的文档: http : //flake8.pycqa.org/en/latest/user/options.html?highlight=per-file-ignores#cmdoption-flake8-per-file-ignores

It is not possible to place a noqa comment for specific codes at the top of a file like you can for individual lines.不可能像对单个行那样在文件顶部放置特定代码的noqa注释。 # flake8: noqa: F401 may at first appear to work, but it's actually being detected as only # flake8: noqa , which means "ignore all messages in the file". # flake8: noqa: F401似乎可以工作,但实际上仅检测到它# flake8: noqa ,这意味着“忽略文件中的所有消息”。

Before version 3.7.0, ignoring specific errors was only implemented per-line but not per-file.在 3.7.0 版本之前,忽略特定错误仅在每行而不是每个文件中实现。

The feature is being tracked and discussed in issue #89 from which only the per-line proposal has been adopted.该功能正在问题 #89 中进行跟踪和讨论,其中仅采用了每行提案。 More recently, an implementation has been proposed in this merge request , which nobody has followed up on.最近,在这个合并请求中提出了一个实现,但没有人跟进。

However, some extensions have emerged to address the problem:但是,已经出现了一些扩展来解决这个问题:

I implemented a flake8 plugin flake8-in-file-ignores to allow adding "ignore" rules in the file itself (as opposed to the built-in config approach), the plugint uses the following syntax我实现了一个 flake8 插件flake8-in-file-ignores以允许在文件本身中添加“忽略”规则(与内置配置方法相反),该插件使用以下语法

# flake8-in-file-ignores: noqa: E731,E123

Update .flake8 file with更新.flake8文件

ignore = E123,E125,H404,H405,H803 ... any other rools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM