Flake8 Black Plugin
flake8-black is an MIT licensed Flake8 plugin designed to integrate Black, the uncompromising code formatter, into the Flake8 ecosystem. It allows developers to validate their Python code style against Black's rules as part of their linting process, typically used in continuous integration or pre-commit hooks. The current version is 0.4.0, and it generally follows Black's release cadence for compatibility updates.
Warnings
- gotcha Black's formatting for slices (e.g., `foo[1:]`) includes a space before the colon, which conflicts with `pycodestyle`'s `E203` error (`whitespace before ':'`). This will cause `flake8` to fail unless `E203` is explicitly ignored in your Flake8 configuration.
- gotcha Black formats line breaks before binary operators, which aligns with PEP 8 as of April 2016. However, `flake8` has a disabled-by-default warning `W503` (`line break before binary operator`) that conflicts with this. Enabling `W503` will lead to false positives.
- breaking Earlier versions of `flake8-black` (before v0.1.2, which implicitly required `black>=19.3b0`) could fail with errors like `__call__() got an unexpected keyword argument 'target_versions'` if used with incompatible `black` versions due to `black` API changes.
- gotcha `flake8-black` does not automatically read or respect `black`'s `include` or `exclude` settings from `pyproject.toml`. If you have files or directories that `black` ignores, you must configure `flake8` to ignore them separately using `exclude` or `per-file-ignores` in your `flake8` configuration.
- gotcha Flake8 itself does not currently use `pyproject.toml` for its configuration. All `flake8` related settings, including those for `flake8-black` or ignoring specific codes, must be placed in a `.flake8`, `setup.cfg`, or `tox.ini` file.
- gotcha The `BLK100` error code from `flake8-black` signifies that `black` would make changes to the file. It does not reformat the file; it only reports that reformatting is needed. If you encounter `BLK100`, it means the file is not formatted according to `black`'s rules.
Install
-
pip install flake8-black
Quickstart
# my_module.py
def my_function( # This line is intentionally long to trigger Black's reformatting
argument_one: str,
argument_two: int,
) -> None:
print(f"Hello {argument_one}, your number is {argument_two}.")
# .flake8 (configuration file in project root)
# [flake8]
# max-line-length = 88
# extend-ignore = E203, W503, E701
# To run flake8 with flake8-black:
# flake8 my_module.py