配置Flake8

Once you have learned how to invoke Flake8, you will soon want to learn how to configure it so you do not have to specify the same options every time you use it.

本节将向您展示如何制作

flake8

请记住,您要指定某些选项而不必编写

flake8 --select E123,W456 --enable-extensions H111

配置位置

Flake8 支持将其配置存储在以下位置:

  • 您的顶级用户目录

  • 在你的项目中 setup.cfgtox.ini.flake8 .

在命令行设置的值具有最高优先级,然后是项目配置文件中的值,然后是用户目录中的值,最后是默认值。但是,还有其他命令行选项可以改变这一点。

“用户”配置

Flake8 允许用户使用“全局”配置文件来存储首选项。用户配置文件应该存储在用户的“home”目录中。

  • 在Windows上,“home”目录类似于 C:\\Users\sigmavirus24 ,也就是说, ~\ .

  • 在Linux和其他类似Unix的系统(包括OS X)上,我们将查看 ~/ .

注意 Flake8 寻找 ~\.flake8 在Windows和 ~/.config/flake8 在Linux和其他Unix系统上。

用户配置文件使用与项目配置文件相同的语法。继续读下去看看语法。

项目配置

Flake8 是在理解人们将项目组织到子目录的基础上编写的。举个例子 Flake8 自己的项目结构

flake8
├── docs
│   ├── build
│   └── source
│       ├── _static
│       ├── _templates
│       ├── dev
│       ├── internal
│       └── user
├── flake8
│   ├── formatting
│   ├── main
│   ├── options
│   └── plugins
└── tests
    ├── fixtures
    │   └── config_files
    ├── integration
    └── unit

在顶层 flake8 目录(其中包含 docsflake8tests )还有 tox.inisetup.cfg 文件夹。在我们的情况下,我们 Flake8 中的配置 tox.ini . 不管你是否保持你的配置 .flake8setup.cfgtox.ini 我们希望您使用INI来配置 Flake8 (因为这些文件中的每一个都已经使用INI作为格式)。这意味着任何 Flake8 要设置的配置必须位于 flake8 部分,这意味着它需要这样开始:

[flake8]

您要在配置文件中指定的每个命令行选项可以通过以下两种方式之一命名:

  1. 使用下划线 (_ )而不是连字符 (-

  2. 简单地使用连字符(没有前导连字符)

注解

不是每一个 Flake8 可以在配置文件中指定命令行选项。看到了吗 our list of options 以确定将从配置文件解析哪些选项。

我们来看看 Flake8 的配置部分:

[flake8]
ignore = D203
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
max-complexity = 10

这相当于:

flake8 --ignore D203 \
         --exclude .git,__pycache__,docs/source/conf.py,old,build,dist \
         --max-complexity 10

在我们的情况下,如果我们愿意,我们也可以这样做

[flake8]
ignore = D203
exclude =
    .git,
    __pycache__,
    docs/source/conf.py,
    old,
    build,
    dist
max-complexity = 10

这允许我们添加注释,说明为什么要排除项目,例如。

[flake8]
ignore = D203
exclude =
    # No need to traverse our git directory
    .git,
    # There's no value in checking cache directories
    __pycache__,
    # The conf file is mostly autogenerated, ignore it
    docs/source/conf.py,
    # The old directory contains Flake8 2.0
    old,
    # This contains our built documentation
    build,
    # This contains builds of flake8 that we don't want to check
    dist
max-complexity = 10

注解

遵循建议的设置 Python's configparserFlake8 不支持任何键的内联注释。所以,虽然这很好:

[flake8]
per-file-ignores =
    # imported but unused
    __init__.py: F401

这不是:

[flake8]
per-file-ignores =
    __init__.py: F401 # imported but unused

注解

如果您使用的是python2,您会注意到我们下载了 configparser 从PyPI返回。这个backport使我们能够在所有受支持的Python版本上支持这种行为。

请这样做 not 将有关此依赖项的问题公开给 Flake8 .

注解

您还可以指定 --max-complexity 作为 max_complexity = 10 .

如果要忽略一长串错误代码,这也很有用。让我们看看项目中的Flake8配置的一部分 tox.ini

[flake8]
# it's not a bug that we aren't using all of hacking, ignore:
# F812: list comprehension redefines ...
# H101: Use TODO(NAME)
# H202: assertRaises Exception too broad
# H233: Python 3.x incompatible use of print operator
# H301: one import per line
# H306: imports not in alphabetical order (time, os)
# H401: docstring should not start with a space
# H403: multi line docstrings should end on a new line
# H404: multi line docstring should start without a leading new line
# H405: multi line docstring summary not separated with an empty line
# H501: Do not use self.__dict__ for string formatting
ignore = F812,H101,H202,H233,H301,H306,H401,H403,H404,H405,H501

他们使用注释来描述支票,但也可以这样写:

[flake8]
# it's not a bug that we aren't using all of hacking
ignore =
    # F812: list comprehension redefines ...
    F812,
    # H101: Use TODO(NAME)
    H101,
    # H202: assertRaises Exception too broad
    H202,
    # H233: Python 3.x incompatible use of print operator
    H233,
    # H301: one import per line
    H301,
    # H306: imports not in alphabetical order (time, os)
    H306,
    # H401: docstring should not start with a space
    H401,
    # H403: multi line docstrings should end on a new line
    H403,
    # H404: multi line docstring should start without a leading new line
    H404,
    # H405: multi line docstring summary not separated with an empty line
    H405,
    # H501: Do not use self.__dict__ for string formatting
    H501

或者他们可以用每个评论来描述 why 他们忽略了支票。 Flake8 知道如何解析这些列表,并将适当地处理这些情况。

使用本地插件

3.5.0 新版功能.

Flake8 允许用户编写本地项目中的插件。这些插件不需要使用setuptools或与PyPI上分布的插件相关的任何其他开销。要使用这些插件,用户必须在其配置文件中指定它们(例如。, .flake8setup.cfgtox.ini ). 必须在名为 flake8:local-plugins .

用户可以配置检查源代码的插件,例如。, extension 插件,以及报告错误的插件,例如。, report 插件。

配置示例如下:

[flake8:local-plugins]
extension =
    MC1 = project.flake8.checkers:MyChecker1
    MC2 = project.flake8.checkers:MyChecker2
report =
    MR1 = project.flake8.reporters:MyReporter1
    MR2 = project.flake8.reporters:MyReporter2

Flake8 但是,也允许用逗号分隔插件,例如:

[flake8:local-plugins]
extension =
    MC1 = project.flake8.checkers:MyChecker1,
    MC2 = project.flake8.checkers:MyChecker2
report =
    MR1 = project.flake8.reporters:MyReporter1,
    MR2 = project.flake8.reporters:MyReporter2

这些配置将允许您选择您自己的定制报告器插件,您已经设计或将利用您的新检查类。

如果您的软件包安装在 Flake8 将从运行,并且您的本地插件是该包的一部分,您已经设置好了; Flake8 将能够导入您的本地插件。但是,如果您正在处理未设置为可安装程序包的项目,或者 Flake8 可能需要指出的是,与运行代码的虚拟机不同 Flake8 从何处导入本地插件。你可以通过 paths 期权在 local-plugins 配置的部分:

[flake8:local-plugins]
extension =
  MC1 = myflake8plugin:MyChecker1
paths =
  ./path/to

相对路径将相对于配置文件进行解释。可以列出多个路径(逗号分隔,就像 exclude )根据需要。如果您的本地插件有任何依赖关系,那么就由您来确保它们安装在任何Python环境中 Flake8 磨合。

注解

这些插件遵循与常规插件相同的准则。