flake8-expression-complexity is an extension for Flake8 that enforces limits on the complexity of individual expressions within Python code. It works by analyzing abstract syntax tree (AST) nodes to score expressions and reports an `ECE001` error when a configurable threshold is exceeded. The current version is 0.0.11, and it maintains a moderate release cadence, with updates addressing new features and bug fixes.
pip install flake8-expression-complexityVerified import paths — ran on the pinned version, not inferred.
After installing, `flake8-expression-complexity` automatically integrates with the `flake8` command. To use it, simply run `flake8` against your Python files. The plugin provides the `ECE001` error code. You can configure the maximum allowed expression complexity using the `--max-expression-complexity` command-line option or in a configuration file (e.g., `setup.cfg`, `tox.ini`, or `.flake8`). The example demonstrates a highly complex `if` condition that would typically trigger an `ECE001` error with a low `max-expression-complexity` setting.
Review the flagged expressions carefully. If the complexity is deemed acceptable for readability, consider increasing the `--max-expression-complexity` threshold or using `# noqa: ECE001` for specific lines. Restructuring chained calls into intermediate variables might also reduce reported complexity.
Adjust the threshold using `--max-expression-complexity=<VALUE>` on the command line or `max-expression-complexity = <VALUE>` in your Flake8 configuration file (`setup.cfg`, `tox.ini`, or `.flake8`). Experiment with values to find a suitable balance for your codebase.
Understand the difference: ECE001 relates to the complexity of a single line/expression, while C901 relates to the number of independent paths through a function. Configure both plugins independently to manage different aspects of code complexity. `flake8-expression-complexity` adds ECE001; `mccabe` adds C901.
Enable the `--ignore-django-orm-queries-complexity` option when running `flake8` or set `ignore-django-orm-queries-complexity = True` in your Flake8 configuration file to prevent false positives on Django ORM queries.