Flake8:忽略整个文件的特定警告 [英] Flake8: Ignore specific warning for entire file

查看:815
本文介绍了Flake8:忽略整个文件的特定警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

忽略错误 文档当前列出了一种忽略特定行的特定错误的方法:

example = lambda: 'example'  # noqa: E731

...以及忽略整个文件的 all 错误的方法:

# flake8: noqa

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

...以及通过配置或通过命令行选项通过全局在整个项目中禁用特定错误的几种方式.

但是,如果我想忽略整个单个文件中的特定错误-例如,要禁用有关__init__.py桶中未使用进口的警告,该怎么办?该文件仅导入了一堆类,以便其他包中的代码可以依次从中导入它们?文档似乎并未暗示任何语法.有可能吗?

解决方案

从Flake8 3.7.0开始,您可以使用--per-file-ignores选项执行此操作.

命令行示例

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

或者在您的配置文件中

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

在此处查看文档:

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? 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.

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

It is not possible to place a noqa comment for specific codes at the top of a file like you can for individual lines. # 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:忽略整个文件的特定警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆