使用 pyproject.toml 和 flake8 指定每个文件忽略

Pio*_*ski 22 python configuration lint flake8

我正在使用 flake8 (与 flakehell 一起使用,但这不会干扰)并将其配置保存在pyproject.toml文件中。我想添加一个per-file-ignores配置,但没有任何效果,并且没有关于如何在 toml 文件中格式化它的文档。

Flake8 文档仅显示“本机”配置文件格式:

per-file-ignores =
    project/__init__.py:F401
    setup.py:E121
    other_project/*:W9
Run Code Online (Sandbox Code Playgroud)

pyproject.toml 没有描述/示例。

我试过:

per-file-ignores=["file1.py:W0621", "file2.py:W0621"]
Run Code Online (Sandbox Code Playgroud)

per-file-ignores={"file1.py" = "W0621", "file2.py" = "W0621"}
Run Code Online (Sandbox Code Playgroud)

两者都默默地失败并且没有任何效果(仍然发出警告)。

使用 pyproject.toml 时,flake8/flakehell 中每个文件忽略设置的正确语法是什么?

Ant*_*ile 43

flake8 不支持 pyproject.toml,仅支持.flake8, setup.cfg, 和tox.ini


免责声明:我是 flake8 维护者

  • 请实施PEP518,它现在是标准的!多谢! (17认同)
  • 请原谅我的无知,但为什么 flake8 不支持 pyproject.toml?项目有 3 个以上的分支如何解决这个问题?这是一场权力游戏还是符合逻辑? (10认同)
  • Python 3.11 中将提供标准库 (tomllib) (5认同)
  • 多谢!我意识到 pyproject.toml 支持实际上来自 flakehell ...在我的例子中,我应该使用 flakehell 异常语法。https://flakehell.readthedocs.io/config.html Flakehell 本身不支持此特定设置。 (2认同)

sir*_*ku6 26

目前,pyproject-flake8允许您像这样编写 flake8 设置pyproject.toml

# pyproject.toml
[tool.flake8]
    exclude = ".venv"
    max-complexity = 10
    max-line-length = 100
    extend-ignore = """
        W503,
        E203,
        E701,
    """
    per-file-ignores = """
        __init__.py: F401
        ./src/*: E402
    """
Run Code Online (Sandbox Code Playgroud)

  • 还有 [ruff](https://github.com/charliermarsh/ruff),它是一个完全不同的 linter,但功能更强大,能够自动修复很多东西,并且内置“pyproject.toml”支持 (9认同)
  • 一些替代方案:[flake9](https://gitlab.com/retnikt/flake9) 和 [flake518](https://github.com/carstencodes/flake518) (4认同)
  • @LordBertson 我该如何表达我的爱,因为你提供了下一级的 linter?不幸的是,我不能对你的评论点赞3次...... (2认同)