Specify per-file-ignores with pyproject.toml and flake8
Asked Answered
I

2

24

I am using flake8 (with flakehell but that should not interfere) and keep its configuration in a pyproject.toml file. I want to add a per-file-ignores config but nothing works and there is no documentation on how it is supposed to be formatted in a toml file.

Flake8 docs show only the 'native' config file format:

per-file-ignores =
    project/__init__.py:F401
    setup.py:E121
    other_project/*:W9

There is no description / example for pyproject.toml.

I tried:

per-file-ignores=["file1.py:W0621", "file2.py:W0621"]

and

per-file-ignores={"file1.py" = "W0621", "file2.py" = "W0621"}

both of which silently fail and have no effect (the warning is still raised).

What is the proper syntax for per-file-ignores setting in flake8/flakehell while using pyproject.toml?

Interdental answered 22/10, 2020 at 12:47 Comment(0)
P
47

flake8 does not have support for pyproject.toml, only .flake8, setup.cfg, and tox.ini


disclaimer: I am the flake8 maintainer

Pallua answered 22/10, 2020 at 17:34 Comment(8)
Thanks a lot! I have realized that the pyproject.toml support was actually from flakehell ... And in my case I should use flakehell Exceptions syntax. flakehell.readthedocs.io/config.html Flakehell itself does not support this particular setting.Interdental
@PiotrZakrzewski , how did it go with the "exceptions syntax"? I'm trying to have something like: ``` [tool.flakehell.exceptions."*/test/.py"] pydocstyle = ["-*"] ``` but it does not exclude that specific plugin for the passed glob pattern :/ ...Echoechoic
does this answer need to be updated if @siruku6 is correct?Intramolecular
@Intramolecular nope, pyproject-flake8 is not official nor supported, nor does it even work correctly (and will break every release as it uses private internal implementation details)Pallua
please implement PEP518, it is standard now! Thanks a lot!Stormystorting
A standard lib will be available in Python 3.11 (tomllib)Stormystorting
@Stormystorting PEP 518 is a standard for packaging tools it is not a standard for "shove your configuration here"Pallua
excuse my ignorance, but why doesn't flake8 support pyproject.toml? How can there be 3+ forks of the project to resolve this issue? Is this a power-game or is it logical?Charr
C
28

Currently, pyproject-flake8 enables you to write your flake8 settings on pyproject.toml like this.

# 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
    """
Cloris answered 3/12, 2021 at 1:59 Comment(4)
Some alternatives: flake9 and flake518Zobe
There is also ruff, which is a different linter altogether, but a bit more powerful with ability to autofix a lot of stuff and pyproject.toml support built-inCasino
@Casino How can I express my love because you provided a next-level linter? Unfortunately, I cannot upvote your comment 3 times...Charity
Unfortunately pyproject-flake8 doesn't work with Python 3.12, while Flake8-pyproject does.Cerium

© 2022 - 2024 — McMap. All rights reserved.