选择并忽略冲突

可以选择并忽略 Flake8 以及我们安装的插件。也有可能在 Flake8 3.0结合使用 flake8 --selectflake8 --ignore . 用户指南的这一章旨在介绍Flake8如何根据不同的输入报告错误。

忽略Flake8的冲突

默认情况下, Flake8 包含忽略的错误代码列表。的某个版本使用的列表 Flake8 可能与不同版本使用的列表不同。要查看默认列表, flake8 --help 将使用当前默认列表显示输出。

扩展默认忽略列表

如果我们想扩展被忽略错误代码的默认列表,我们可以使用 flake8 --extend-ignore 在命令行上为特定运行指定逗号分隔的代码列表,例如。,

flake8 --extend-ignore=E1,E23 path/to/files/ path/to/more/files

这说明 Flake8 忽略以开头的任何错误代码 E1E23 ,以及默认的忽略列表。要查看默认错误代码忽略列表,请运行 flake8 --help 并参考帮助文本 flake8 --ignore .

重写默认忽略列表

如果我们想的话 完全地 重写忽略的错误代码的默认列表,我们可以使用 flake8 --ignore 要为命令行上的特定运行指定以逗号分隔的代码列表,例如。,

flake8 --ignore=E1,E23,W503 path/to/files/ path/to/more/files/

这说明 Flake8only 忽略以开头的错误代码 E1E23W503 当它运行的时候。

注解

文件 flake8 --ignore 显示如何更改配置文件中的忽略列表的示例。另请参见 配置Flake8 以及有关如何使用配置文件的详细信息。

在线忽略错误

在某些情况下,我们可能不想忽略整个项目的错误代码(或一类错误代码)。相反,我们可能希望忽略特定行上的特定错误代码。让我们举一个例子

example = lambda: 'example'

有时候我们真的需要这么简单的东西。我们可以像平常一样定义一个函数。注意,在某些情况下,这会分散对实际发生的事情的注意力。在这些情况下,我们还可以:

example = lambda: 'example'  # noqa: E731

这只会忽略pycodestyle的错误,该错误检查lambda赋值并生成 E731 . 如果线上有其他错误,则会报告这些错误。 # noqa 不区分大小写,后面没有冒号 # noqa 会被忽略。

注解

如果我们想禁用 Flake8 尊重 # noqa 评论,我们可以参考 flake8 --disable-noqa .

如果我们想要忽略的错误不止一个,我们可以列出所有错误,并用逗号分隔它们:

# noqa: E731,E123

最后,如果我们有一行特别糟糕的代码,我们可以使用 # noqa 之后什么都没有。

前后内容 # noqa: ... 部分被忽略,因此多个注释可能出现在一行上。以下是几个例子:

# mypy requires `# type: ignore` to appear first
x = 5  # type: ignore  # noqa: ABC123

# can use to add useful user information to a noqa comment
y = 6  # noqa: ABC456  # TODO: will fix this later

忽略整个文件

想象一下我们正在添加 Flake8 到一个代码库。让我们进一步设想,除了一些特别糟糕的文件,我们可以添加 Flake8 轻松地继续我们的生活。有两种方法可以忽略该文件:

  1. 通过显式地将其添加到排除路径列表中(请参见: flake8 --exclude

  2. 通过添加 # flake8: noqa 文件

前者是 推荐 忽略整个文件的方法。通过使用排除列表,我们可以将其包含在配置文件中,并有一个中心位置来查找哪些文件未包含在其中 Flake8 检查。后者的好处是当我们运行时 Flake8 具有 flake8 --disable-noqa 该文件中的所有错误都将显示出来,而无需修改配置。两者都存在,所以我们可以选择哪个对我们更好。

使用Flake8选择冲突

Flake8 有一个默认的违规类列表。此列表为:

  • C90

    所有 C90 当用户指定 flake8 --max-complexity

  • E

    所有 E 类冲突是pycodestyle报告的“错误”

  • F

    所有 F 类冲突由pyflakes报告

  • W

    所有 W 类冲突是pycodestyle报告的“警告”

可以通过指定 flake8 --select . 就像指定 flake8 --ignore 会改变 Flake8 ,也会的 flake8 --select .

让我们使用以下示例代码查看一些示例:

# example.py
def foo():
    print(
                "Hello"
        "World"
        )

默认情况下,如果我们运行 flake8 在这个文件中,我们将得到:

flake8 example.py
example.py:4:9: E131 continuation line unaligned for hanging indent

现在让我们选择全部 E 班级违规:

flake8 --select E example.py
example.py:3:17: E126 continuation line over-indented for hanging indent
example.py:4:9: E131 continuation line unaligned for hanging indent
example.py:5:9: E121 continuation line under-indented for hanging indent

突然之间,报告给我们的错误要多得多。使用 --select 单独将覆盖默认值 --ignore 列表。在这些情况下,用户告诉我们他们想要所有 E 因此我们忽略默认忽略的违规列表。

我们也可以非常具体。例如,我们可以

flake8 --select E121 example.py
example.py:5:9: E121 continuation line under-indented for hanging indent

我们还可以在命令行和配置文件中指定要选择的项列表。

flake8 --select E121,E131 example.py
example.py:4:9: E131 continuation line unaligned for hanging indent
example.py:5:9: E121 continuation line under-indented for hanging indent

取之有利

之前 Flake8 3.0,所有处理 flake8 --selectflake8 --ignore 已委派给pycodestyle。它对选项的处理与 Flake8 已经设计了3.0。

pycodestyle一直是首选 --ignore 结束 --select 会忽略 --select 如果用户同时提供。 Flake8 3.0现在将尽最大努力直观地组合用户提供的两个选项。让我们看一些例子,使用:

# example.py
import os


def foo():
    var = 1
    print(
                "Hello"
        "World"
        )

如果我们逃跑 Flake8 默认设置为:

flake8 example.py
example.py:1:1: F401 'os' imported but unused
example.py:5:5: F841 local variable 'var' is assigned to but never used
example.py:8:9: E131 continuation line unaligned for hanging indent

现在让我们选择全部 EF 包括默认忽略列表中的冲突。

flake8 --select E,F example.py
example.py:1:1: F401 'os' imported but unused
example.py:5:5: F841 local variable 'var' is assigned to but never used
example.py:7:17: E126 continuation line over-indented for hanging indent
example.py:8:9: E131 continuation line unaligned for hanging indent
example.py:9:9: E121 continuation line under-indented for hanging indent

现在,让我们在选择其余部分时有选择地忽略其中一些:

flake8 --select E,F --ignore F401,E121 example.py
example.py:5:5: F841 local variable 'var' is assigned to but never used
example.py:7:17: E126 continuation line over-indented for hanging indent
example.py:8:9: E131 continuation line unaligned for hanging indent

Via this example, we can see that the most specific user-specified rule will win. So in the above, we had very vague select rules and two very specific ignore rules. Let's look at a different example:

flake8 --select F401,E131 --ignore E,F example.py
example.py:1:1: F401 'os' imported but unused
example.py:8:9: E131 continuation line unaligned for hanging indent

在本例中,我们看到,由于我们选择的违规代码更具体,因此报告了这些代码。