20

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?

Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28

2 Answers2

38

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


disclaimer: I am the flake8 maintainer

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 2
    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. https://flakehell.readthedocs.io/config.html Flakehell itself does not support this particular setting. – Piotr Zakrzewski Oct 23 '20 at 07:53
  • @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 :/ ... – castarco Aug 05 '21 at 12:23
  • 1
    does this answer need to be updated if @siruku6 is correct? – Zaffer Dec 22 '21 at 14:50
  • @Zaffer 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) – anthony sottile Dec 22 '21 at 17:37
  • 9
    please implement PEP518, it is standard now! Thanks a lot! – marscher Jun 15 '22 at 10:10
  • 4
    A standard lib will be available in Python 3.11 (tomllib) – marscher Jun 15 '22 at 10:23
  • @marscher PEP 518 is a standard **for packaging tools** it is not a standard for "shove your configuration here" – anthony sottile Jun 15 '22 at 13:07
  • 1
    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? – Oliver Angelil Aug 17 '23 at 13:36
21

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
    """
siruku6
  • 339
  • 3
  • 7
  • 3
    Some alternatives: [flake9](https://gitlab.com/retnikt/flake9) and [flake518](https://github.com/carstencodes/flake518) – Person Apr 09 '22 at 15:05
  • 6
    There is also [ruff](https://github.com/charliermarsh/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-in – LordBertson Dec 30 '22 at 13:40
  • 1
    @LordBertson How can I express my love because you provided a next-level linter? Unfortunately, I cannot upvote your comment 3 times... – Péter Szilvási Aug 16 '23 at 12:57