将备用 flake8 规则应用于文件子集

Applying alternate flake8 rules to a subset of files

我有一个项目,我在其中使用 tox 进行测试,运行s flake8 测试。我想将不同的 flake8 配置应用于我的测试目录,而不是其他任何东西;我想在我的测试中忽略 E402,因为我在导入要测试的模块之前弄乱了 sys.path

flake8 配置语法只允许您将一种配置应用于 include/exclude 匹配的文件,因此我添加了 ./tests/.flake8 以添加仅适用于这些文件的配置。

./tox.ini

[tox]
envlist = lint, py27, py36

[testenv]
commands =
    coverage run --source=myModule -a setup.py test

[testenv:lint]
basepython = python3
ignore_errors = True
deps =
    -r{toxinidir}/requirements_test.txt
commands =
    flake8
    pylint myModule
    pydocstyle myModule tests

[flake8]
count = true
statistics = True

./tests/.flake8

[flake8]
ignore = E402

使用我的 tox.ini 文件中的 flake8 选项,点文件总是被忽略。 tox.ini 文件中没有选项,flake8 运行 从命令行使用 dotfile,但当它被 tox 运行 时被忽略。

看起来没有办法对同一项目下的不同文件集应用不同的flake8配置。我是否错过了配置语法中的某些内容,这些内容允许我在此处执行我想做的事情?

您可以尝试 运行 flake8 两次 — 一次使用不包括 tests/ 的全局配置,第二次 运行 仅在 tests/ 中:

commands =
    flake8 --exclude=tests
    cd tests && flake8

从版本 3.7.0 开始 flake8 now includes 一个标志来做你想做的事:per-file-ignores。要在您的配置文件中使用它,请执行以下操作:

[flake8]
per-file-ignores =
    tests/*: E402

它也可以在命令行上应用,方法是在您的 flake8 调用中添加这样的标志:

--per-file-ignores=tests/*.py:E402