首页 > 解决方案 > 将备用 flake8 规则应用于文件子集

问题描述

我有一个项目,我使用 tox 进行测试,它运行 flake8 测试。我想对我的测试目录应用不同的 flake8 配置;我想在我的测试中忽略 E402,因为我sys.path在导入要测试的模块之前搞砸了。

flake8 配置语法只允许您将一个配置应用于包含/排除匹配的文件,因此我./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 配置应用于同一项目下的不同文件集。我是否错过了配置语法中允许我在这里做我想做的事情的东西?

标签: pythontoxflake8

解决方案


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

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

它也可以通过在您的flake8调用中添加这样的标志来应用在命令行上:

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

推荐阅读