Repolode
adamchainz

adamchainz/flake8-comprehensions

A flake8 plugin that flags unnecessary or inefficient list, set, dict, and generator comprehension and builtin call patterns.

Stars
466
Forks
25
Open issues
11
Last commit
2 days ago

What is it?

flake8-comprehensions is a plugin for flake8, the Python linter, that catches patterns where comprehensions, generator expressions, or calls to list(), set(), dict(), and tuple() are redundant or can be written more directly. Examples include wrapping a generator in list() when a list comprehension would do, or calling dict() around a dict literal.

Each check is a numbered rule in the C4 family, such as C400 for unnecessary generators and C419 for list comprehensions inside any() or all() that prevent short-circuiting. Rules can be enabled through flake8's select setting by adding the C4 prefix. The plugin requires Python 3.11 or later and is installed with pip.

Key features

  • 01Flags redundant list(), set(), dict() and tuple() calls around generators, comprehensions and literals.
  • 02Detects list comprehensions in any() and all() that prevent short-circuiting.
  • 03Suggests dict.fromkeys() for dict comprehensions that assign one constant value.
  • 04Recommends replacing lambda-based map() calls with comprehensions or generator expressions.
  • 05Integrates with flake8 and is enabled by default unless the select setting is customized.

Use cases

  • Keeping a Python codebase clean in a CI pipeline that already runs flake8.
  • Reviewing code for needless intermediate lists or double conversions during refactoring.
  • Teaching team members idiomatic comprehension style through automated lint feedback.

Quick start

  1. 1

    Install the plugin with pip

    python -m pip install flake8-comprehensions
  2. 2

    If you set Flake8's select option, add the C4 prefix to it; otherwise the plugin is active by default

Things to know

  • Requires Python 3.11 to 3.15.

In collections

Topics

FAQ

What is flake8-comprehensions?
A flake8 plugin that flags unnecessary or inefficient list, set, dict, and generator comprehension and builtin call patterns. flake8-comprehensions is a plugin for flake8, the Python linter, that catches patterns where comprehensions, generator expressions, or calls to list(), set(), dict(), and tuple() are redundant or can be written more directly. Examples include wrapping a generator in list() when a list comprehension would do, or calling dict() around a dict literal.
Is flake8-comprehensions free to use? What license does it have?
flake8-comprehensions is open source under the MIT license. Check the license terms before using it in commercial products.
Is flake8-comprehensions still maintained?
Actively maintained. The last commit to flake8-comprehensions was on Nov 4, 2026.
What can I use flake8-comprehensions for?
Keeping a Python codebase clean in a CI pipeline that already runs flake8. Reviewing code for needless intermediate lists or double conversions during refactoring. Teaching team members idiomatic comprehension style through automated lint feedback.
How do I install flake8-comprehensions?
Install the plugin with pip: python -m pip install flake8-comprehensions If you set Flake8's select option, add the C4 prefix to it; otherwise the plugin is active by default
Where can I get flake8-comprehensions?
The source code and installation instructions are on GitHub: https://github.com/adamchainz/flake8-comprehensions

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects