Install & Compatibility
Where this runs
tested against v3.17.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.3MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.9s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
flake8-comprehensions
✓ This is a Flake8 plugin and is not imported directly into Python code. It integrates automatically with Flake8 after installation.
Plugins extend Flake8's functionality by registering themselves; no explicit Python `import` statement is needed in user code.
After installing `flake8` and `flake8-comprehensions`, run `flake8` on your Python files. The plugin will automatically integrate. If you use Flake8's `select` option, ensure you include the `C4` prefix to enable its checks.
import os
def analyze_code(file_content):
# Simulate a file for flake8
with open('temp_code.py', 'w') as f:
f.write(file_content)
# Run flake8 with the plugin
# In a real scenario, you'd run 'flake8 temp_code.py' from the command line
# This example demonstrates the plugin's effect.
print(f"Running flake8 on:\n---\n{file_content}\n---")
# To actually run flake8 programmatically and capture output,
# one would typically use subprocess.run(['flake8', 'temp_code.py'], capture_output=True)
# For this quickstart, we'll illustrate the problematic code.
# Example of code that flake8-comprehensions would flag
bad_list_comp = """
def get_numbers():
items = range(5)
# C400: Unnecessary generator - rewrite as a list comprehension
result = list(x for x in items)
# C403: Unnecessary list comprehension - rewrite as a set comprehension
another = set([x * 2 for x in items])
# C404: Unnecessary list comprehension - rewrite as a dict comprehension
mapping = dict([(i, str(i)) for i in items])
# C416: Unnecessary list comprehension - rewrite using list()
unchanged = [x for x in items]
return result, another, mapping, unchanged
"""
# In a terminal, after 'pip install flake8 flake8-comprehensions':
# Save the above 'bad_list_comp' content to 'my_module.py'
# Then run: flake8 my_module.py
# This will output warnings like:
# my_module.py:5:14: C400 Unnecessary generator - rewrite as a list comprehension.
# my_module.py:7:17: C403 Unnecessary list comprehension - rewrite as a set comprehension.
# my_module.py:9:17: C404 Unnecessary list comprehension - rewrite as a dict comprehension.
# my_module.py:11:15: C416 Unnecessary list comprehension - rewrite using list().
# For illustrative purposes, not actual execution of flake8
# analyze_code(bad_list_comp)
Debug
Known issues
breakingVersion 3.0.0 dropped support for Python 2; only Python 3.0+ was supported at that time. Later versions, including 3.17.0, require Python 3.9 and above.fixEnsure your project is running on Python 3.9 or higher. Upgrade Flake8 to a compatible version (3.0+).
affects: >=3.0.0
breakingThe `__version__` attribute was removed in version 3.0.1. Directly accessing `flake8_comprehensions.__version__` will fail.fixUse `importlib.metadata.version("flake8-comprehensions")` to programmatically retrieve the installed version. affects: >=3.0.1
gotchaThe automatic fix suggested for rule C416 ('Unnecessary comprehension - rewrite using dict()/list()/set()') can be unsafe for dictionary comprehensions that iterate over a mapping. The `dict()` constructor behaves differently with sequences versus mappings, potentially leading to incorrect refactorings or dropped comments.fixManually review and verify any automatic fixes for C416, especially those involving dictionary comprehensions, to ensure the behavior is preserved and no comments are lost.
affects: All versions with C416
gotchaIf you have an explicit `select` configuration in your Flake8 settings (e.g., `setup.cfg`, `pyproject.toml`, `.flake8`), you must explicitly add the `C4` prefix to your selected codes for `flake8-comprehensions` rules to be active. Otherwise, the plugin is active by default.fixAdd `C4` to your Flake8 `select` configuration, for example: `select = E,F,W,C4`.
affects: All versions
Upgrade
Version history
3.17.0latest on PyPI · released Sep 9, 2025
Audit
Dependencies
flake8requiredThis is a plugin for Flake8 and requires Flake8 to function.