flake8-comprehensions
A flake8 plugin to help you write better list/set/dict comprehensions.
What it is and what it does
flake8-comprehensions is a flake8 plugin that analyzes Python code to identify and flag inefficient or redundant patterns involving comprehensions, generators, and built-in type constructors. It enforces a suite of rules (C400–C417) that catch common mistakes: unnecessary generator expressions wrapped in list/set/dict calls, list comprehensions passed to set/dict constructors, redundant literal syntax, and inefficient double-casting of iterables. The plugin integrates directly into flake8's linting pipeline.
When installed, it runs automatically as part of flake8's checks (unless explicitly disabled via configuration). It emits specific error codes and suggestions for how to rewrite each pattern more idiomatically—for example, replacing `list(x for x in foo)` with `[x for x in foo]` or `set([1, 2])` with `{1, 2}`. The plugin is particularly useful in codebases that prioritize readability and performance, helping developers adopt Pythonic idioms consistently.
Use it for:
- Enforce consistent comprehension style across a team codebase during CI/CD linting.
- Catch performance anti-patterns like unnecessary generator wrapping or double-casting in code review.
- Teach Python developers idiomatic comprehension syntax and when to use set/dict literals.
- Identify redundant list comprehensions that can be simplified to direct set() or dict() calls.
- Flag inefficient sorted() or reversed() patterns that can be optimized with built-in arguments.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A flake8 plugin that detects and suggests rewrites for inefficient or redundant list, set, and dict comprehensions and related constructs.
Yes. The plugin is actively maintained, has no known vulnerabilities, and low install friction. It fills a specific and well-defined niche—catching comprehension anti-patterns—that most general linters do not cover. Install it if your team values code idiomaticity and performance-conscious Python style.
Install
flake8-comprehensions on PyPI
pip
pip install flake8-comprehensionsuv
uv add flake8-comprehensionspoetry
poetry add flake8-comprehensionsInstalling flake8-comprehensions
Before you install
Low friction: a pure-Python wheel with only flake8 as a runtime dependency. Actively maintained with a recent release and no known vulnerabilities.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and commercial projects.
Quickstart
pip install flake8-comprehensions
Then add C4 to flake8's select setting if defined, or it activates by default. Run flake8 on your code to see violations like C400-C417.
Requires Python 3.9 or later.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — flake8 |
| Maintenance | actively maintained — 339 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,872,035/month — #3,471 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: flake8_comprehensions-3.17.0-py3-none-any.whl
Keywords: comprehensions, dict comprehension, flake8, list comprehension, set comprehension
Tags
More Quality Assurance packages
Coverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
ruffRuff is a Python linter and code formatter…
permissive · top 1,000 on PyPI
pexpectPexpect spawns and controls interactive console…
permissive · top 1,000 on PyPI
blackBlack reformats Python source code to a…
permissive · top 1,000 on PyPI
pytest-xdistpytest-xdist distributes pytest tests across…
permissive · top 1,000 on PyPI
cfn-lintValidates AWS CloudFormation templates in YAML…
permissive · top 1,000 on PyPI
flake8-pieA flake8 plugin that detects code quality…
permissive · top 15,000 on PyPI
flake8-returnA flake8 plugin that detects and flags…
permissive · top 15,000 on PyPI
flake8-bugbearA flake8 plugin that detects likely bugs and…
permissive · top 5,000 on PyPI
flake8_simplifyA flake8 plugin that detects and flags…
permissive · top 15,000 on PyPI
flake8-tupleA flake8 plugin that detects unintended…
permissive · top 15,000 on PyPI
flake8-tidy-importsA flake8 plugin that detects and reports…
permissive · top 15,000 on PyPI
flake8-picky-parenthesesA flake8 plugin that detects and reports…
permissive · top 15,000 on PyPI
flake8-implicit-str-concatA Flake8 plugin that detects and reports…
permissive · top 15,000 on PyPI
flake8-commasA flake8 plugin that enforces trailing comma…
permissive · top 15,000 on PyPI
cython-lintA linter for Cython files that checks for…
permissive · top 5,000 on PyPI