Using Flake8 and pyproject.toml with FlakeHell

bowmanjd

Jonathan Bowman

Posted on August 17, 2020

Using Flake8 and pyproject.toml with FlakeHell

More and more, I am using Python tools like Poetry and Black that use pyproject.toml as a central configuration file for packaging and tools. The finalized PEP 518 defined the specification for pyproject.toml, and many tools have adopted it.

Unfortunately, two tools I use frequently do not yet support configuration through pyproject.toml: Flake8 and Mypy.

Thus far, I have not found any option for Mypy other than setup.cfg or mypy.ini, but I have found a solution for Flake8.

Enter FlakeHell.

FlakeHell is a Flake8 wrapper. Configuration is handled in pyproject.toml and can enable/disable specific Flake8 plugins, ignore specific errors, and handle files differently.

Example configuration

Here is my current configuration in pyproject.toml:

[tool.flakehell]
exclude = ["README.rst", "README.md"]
format = "colored"
max_line_length = 88
show_source = true
whitelist = "../../allowlist.txt"

[tool.flakehell.plugins]
flake8-bandit = ["+*", "-S322"]
flake8-bugbear = ["+*"]
flake8-builtins = ["+*"]
flake8-comprehensions = ["+*"]
flake8-darglint = ["+*"]
flake8-docstrings = ["+*"]
flake8-eradicate = ["+*"]
flake8-isort = ["+*"]
flake8-mutable = ["+*"]
flake8-pytest-style = ["+*"]
flake8-spellcheck = ["+*"]
mccabe = ["+*"]
pep8-naming = ["+*"]
pycodestyle = ["+*"]
pyflakes = ["+*"]
pylint = ["+*"]
Enter fullscreen mode Exit fullscreen mode

Configure plugins

The above demonstrates that configuration can be passed from FlakeHell to the relevant plugin, such as the whitelist variable, a part of flake8-spellcheck.

Ignore specific warnings

Also note that certain codes (such as Bandit's S322 warning about input() that is not relevant in Python 3) can be ignored by prefixing them with a - and adding them to the list.

Pretty output

FlakeHell can format the output in various ways, including colorizing, grouping, or JSON-formatting.

Drop-in replacement for flake8

Thankfully, there is flake8helled, a command that replaces flake8, making it easy to configure your editor to use FlakeHell in place of Flake8.

Useful commands

When using FlakeHell, I frequently use the following commands:

  • flakehell lint runs the linter, similar to the flake8 command
  • flakehell plugins lists all the plugins used, and their configuration status
  • flakehell missed shows any plugins that are in the configuration but not installed properly (such as with pip or poetry add -D)
  • flakehell code S322 (or any other code) shows the explanation for that specific warning code.

See FlakeHell's documentation for more direction and ideas.

Happy linting.

💖 💪 🙅 🚩
bowmanjd
Jonathan Bowman

Posted on August 17, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related