{"id":2950,"library":"flake8-comprehensions","title":"Flake8 Comprehensions","description":"A Flake8 plugin designed to help developers write more efficient and Pythonic list, set, and dictionary comprehensions. It detects common anti-patterns and suggests idiomatic alternatives. The current version is 3.17.0, and it maintains a regular release cadence with updates to rules and Python compatibility.","status":"active","version":"3.17.0","language":"en","source_language":"en","source_url":"https://github.com/adamchainz/flake8-comprehensions","tags":["flake8","linter","code-quality","comprehensions","python"],"install":[{"cmd":"pip install flake8-comprehensions","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"This is a plugin for Flake8 and requires Flake8 to function.","package":"flake8","optional":false}],"imports":[{"note":"Plugins extend Flake8's functionality by registering themselves; no explicit Python `import` statement is needed in user code.","symbol":"flake8-comprehensions","correct":"This is a Flake8 plugin and is not imported directly into Python code. It integrates automatically with Flake8 after installation."}],"quickstart":{"code":"import os\n\ndef analyze_code(file_content):\n    # Simulate a file for flake8\n    with open('temp_code.py', 'w') as f:\n        f.write(file_content)\n    \n    # Run flake8 with the plugin\n    # In a real scenario, you'd run 'flake8 temp_code.py' from the command line\n    # This example demonstrates the plugin's effect.\n    print(f\"Running flake8 on:\\n---\\n{file_content}\\n---\")\n    # To actually run flake8 programmatically and capture output, \n    # one would typically use subprocess.run(['flake8', 'temp_code.py'], capture_output=True)\n    # For this quickstart, we'll illustrate the problematic code.\n\n\n# Example of code that flake8-comprehensions would flag\nbad_list_comp = \"\"\"\ndef get_numbers():\n    items = range(5)\n    # C400: Unnecessary generator - rewrite as a list comprehension\n    result = list(x for x in items)\n    # C403: Unnecessary list comprehension - rewrite as a set comprehension\n    another = set([x * 2 for x in items])\n    # C404: Unnecessary list comprehension - rewrite as a dict comprehension\n    mapping = dict([(i, str(i)) for i in items])\n    # C416: Unnecessary list comprehension - rewrite using list()\n    unchanged = [x for x in items]\n    return result, another, mapping, unchanged\n\"\"\"\n\n# In a terminal, after 'pip install flake8 flake8-comprehensions':\n# Save the above 'bad_list_comp' content to 'my_module.py'\n# Then run: flake8 my_module.py\n# This will output warnings like:\n# my_module.py:5:14: C400 Unnecessary generator - rewrite as a list comprehension.\n# my_module.py:7:17: C403 Unnecessary list comprehension - rewrite as a set comprehension.\n# my_module.py:9:17: C404 Unnecessary list comprehension - rewrite as a dict comprehension.\n# my_module.py:11:15: C416 Unnecessary list comprehension - rewrite using list().\n\n# For illustrative purposes, not actual execution of flake8\n# analyze_code(bad_list_comp)\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your project is running on Python 3.9 or higher. Upgrade Flake8 to a compatible version (3.0+).","message":"Version 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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use `importlib.metadata.version(\"flake8-comprehensions\")` to programmatically retrieve the installed version.","message":"The `__version__` attribute was removed in version 3.0.1. Directly accessing `flake8_comprehensions.__version__` will fail.","severity":"breaking","affected_versions":">=3.0.1"},{"fix":"Manually review and verify any automatic fixes for C416, especially those involving dictionary comprehensions, to ensure the behavior is preserved and no comments are lost.","message":"The 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.","severity":"gotcha","affected_versions":"All versions with C416"},{"fix":"Add `C4` to your Flake8 `select` configuration, for example: `select = E,F,W,C4`.","message":"If 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}