skillfed

flake8-comprehensions

A flake8 plugin to help you write better list/set/dict comprehensions.

flake8-comprehensions v3.17.0 1.9M downloads/30d#3,471 on PyPI465
Permissive license MIT Active released

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-comprehensions

uv

uv add flake8-comprehensions

poetry

poetry add flake8-comprehensions

Installing 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

Development Status :: 5 - Production/StableFramework :: Flake8Intended Audience :: DevelopersNatural Language :: EnglishProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Typing :: Typed

Tags

flake8 comprehension lintinglist set dict comprehension checkerpython comprehension optimizationflake8 code quality plugindetect unnecessary generatorscomprehension best practicesflake8 performance linting
flake8-plugincode-stylelinting

More Quality Assurance packages