Flake8
What is Flake8?
Flake8 is an essential linter for Python developers looking to maintain high code quality standards. It's a command-line utility that checks Python code against coding style (PEP 8), programming errors, and complex constructs. What sets Flake8 apart is its extensible nature, allowing integration with a variety of plugins to expand its capabilities.
Flake8 primarily deals with .py
files. It analyzes Python source code files for issues. It can also check configuration files like tox.ini
or .flake8
for setup configuration.
Installing Flake8
With Trunk Check, you can automatically install and configure Flake8 along with any relevant linters in a few straightforward steps. Here's how:
First, if you haven't already installed Trunk CLI, you can do so with the command below:
1curl https://get.trunk.io -fsSL | bash
Next, you can initialize Trunk from the root of your git repository:
1trunk init
This command will scan your repository and create a .trunk/trunk.yaml
file that enables all linters, formatters, and security analyzers, recommended by Trunk Check. This includes Flake8 if applicable to your project.
To see all available linters Trunk Check installed, simply run:
1trunk check list
If you find Flake8 is not automatically enabled, you can do so by running:
1trunk check enable flake8
Alternatively, to disable flake8 run the command below. To disable other tooling applied by Trunk Check, simply replace flake8
with the respective tool you're looking to disable.
1trunk check disable flake8
For more details on Trunk Check setup, see here.
Configuring Flake8
Most linters provide some mechanism to tweak their configuration, e.g. .eslintrc
or Cargo.toml
. Trunk is aware of all the ways individual tools are configured and supports them. This means linters you've already configured will continue to work exactly the same, just now supercharged by Trunk Check.
If you do not have a custom config for Flake8, you'll be on our default configuration. You can check out our open-source configs repo for our always up-to-date collection of sane linter configurations.
The process for adjusting configuration and enabling plugins for Flake8 is straightforward. Flake8 has a plugin architecture where if you install a plugin, it gets used. You can enable Flake8 plugins via:
1enabled:2 - flake8@3.9.2:3 packages:4 - flake8-bugbear@21.4.3
Flake8-bugbear is probably the most popular flake8 plugin and we recommend it. Here are a few other popular flake8 plugins you should consider.
flake8-comprehensions: Helps in identifying unnecessary comprehensions in your code.
flake8-docstrings: Checks for compliance with Python docstring conventions.
flake8-import-order: Checks the order of your imports according to various configurable ordering styles.
Here's an updated code snippet with the above Plugins enabled:
1enabled:2 - flake8@3.9.2:3 packages:4 - flake8-bugbear@21.4.35 - flake8-docstrings@1.7.06 - flake8-import-order@0.18.27 - flake8-comprehensions@3.14.0
Since Flake8 is a Python linter, it's relevant to call out that Trunk uses hermetic runtime versions which you can override if needed. If you're using a newer version of Python than our default (3.10.8 at the time of writing) you can override it in trunk.yaml
via:
1runtimes:2 enabled:3 - python@3.10.8
As always, you can view the defaults and configuration of everything Trunk runs via trunk config print
. Note that Python runtime versions specifically are allowlisted to a limited set.
If you're interested in other tools, check out our open-source repository to see how we define and support 90+ linters and formatters.
Running Flake8
To check your code with Flake8, simply run the command below. This command executes Flake8, along with any other linters Trunk Check has enabled on files you've modified.
1trunk check
If you prefer to check files you've modified with only flake8, run the following:
1trunk check --filter=flake8
Although we'd recommend against it depending on the size of your repository, you can check all files with Flake8 by running the command below.
1trunk check --all --filter=flake8
In most scenarios, you'll want to execute against modified files. Since Trunk is git-aware, it knows what you've changed, and by adding batched execution and caching, you end up with a much faster and smoother way to run Flake8 and other tools.
Updating Trunk Check & Flake8
To upgrade the Trunk CLI along with all plugins and linters in your trunk.yaml
simply run:
1trunk upgrade
We highly recommend running on the latest validated versions of tools as updates will frequently include important security fixes and additional valuable checks. Trunk only auto-suggests linter upgrades to versions that we have tested and support, so you may see a slight lag time when a new linter version is released.
Upgrade will also recommend new tools that have become applicable since the last time your repository was scanned. This can be a result of using new technologies in your repository or trunk itself adding support for more tools. If you don't like a particular recommendation you can always run trunk check disable <linter>
to teach trunk not to recommend it.
Recommended Linters to Pair with Flake8
While Flake8 is a powerful tool on its own, pairing it with other linters can enhance your coding workflow. These linters complement Flake8:
Black: An uncompromising code formatter that works well with Flake8 for consistent code styling.
isort: For sorting and organizing Python imports in a standardized format.
MyPy: A static type checker that can be used alongside Flake8 for type validation.
Linters Flake8 Replaces
Flake8's versatility means it can often replace other linters, streamlining your toolkit. Flake8 makes these specific linters less essential:
Pylint: Flake8 covers many checks done by Pylint, particularly related to code style and syntax errors.
pep8: Since Flake8 already enforces PEP 8 standards, a separate PEP 8 linter is redundant.
pyflakes: Flake8 integrates pyflakes for basic error checking, making a separate pyflakes installation unnecessary.