Flake8-pyproject
Flake8-pyproject is a Flake8 plugin that enables Flake8 to load its configuration from `pyproject.toml` files, a feature not natively supported by Flake8. It registers itself as a plugin, allowing the standard `flake8` command to seamlessly use `pyproject.toml` when present in the current directory. The library also provides an alternative `flake8p` command. It is currently at version 1.2.4 and receives updates primarily for Flake8 compatibility and new configuration options.
Warnings
- breaking Flake8-pyproject versions have specific compatibility with Flake8 major versions. Version 1.0.0 dropped support for Flake8 4.x (requiring Flake8 5.0+), while earlier versions (e.g., 0.9.1) explicitly pinned to Flake8 4.x or earlier. Ensure your `flake8-pyproject` version matches your `flake8` installation.
- gotcha The `pyproject.toml` file containing the `[tool.flake8]` configuration must typically be located in the current working directory where `flake8` is executed. If it's in a different location or named differently, you must explicitly specify its path using the `--toml-config` command-line option.
- gotcha The `[tool.flake8]` section within `pyproject.toml` must adhere to TOML's syntax for arrays and dictionaries (e.g., `ignore = ["E203", "W503"]`, `per-file-ignores = ["__init__.py:F401"]`). This differs from the INI format used in `.flake8` or `setup.cfg`.
- gotcha For Python versions older than 3.11, the `tomli` package is a required dependency for parsing TOML files. Python 3.11 and later include `tomllib` in the standard library, making `tomli` unnecessary.
- gotcha As of version 1.2.3, the plugin specifically parses command-line arguments as passed down from Flake8 and explicitly ignores arguments other than `--toml-config`. This change might affect custom integrations or scripts that rely on specific command-line argument handling by `flake8-pyproject`.
- gotcha Flake8-pyproject works by 'monkey-patching' Flake8 and Python's `configparser` library. This approach, while effective, means the plugin's stability can be sensitive to significant restructuring in future versions of Python or Flake8.
Install
-
pip install Flake8-pyproject -
pip install flake8 'Flake8-pyproject<1.0.0' # For Flake8 4.x pip install flake8 'Flake8-pyproject>=1.0.0' # For Flake8 5.x and later
Quickstart
mkdir my_project
cd my_project
pip install flake8 Flake8-pyproject
# Create pyproject.toml
cat <<EOF > pyproject.toml
[tool.flake8]
ignore = ["E203", "W503"]
max-line-length = 88
per-file-ignores = [
"__init__.py:F401",
]
EOF
# Create a Python file with a linter error
cat <<EOF > my_module.py
import os
def my_func ( arg1, arg2 ):
return arg1 + arg2
EOF
# Run flake8 (will use pyproject.toml config)
flake8 my_module.py
# Alternatively, use the provided flake8p command
# flake8p my_module.py