{"id":7230,"library":"flake8-cognitive-complexity","title":"flake8-cognitive-complexity","description":"An extension for flake8 that validates cognitive functions complexity. Cognitive complexity is an analog of cyclomatic complexity, measuring how difficult a piece of code is to understand, as introduced by G. Ann Campbell and used by SonarSource and CodeClimate. The library is currently at version 0.1.0 and has seen limited releases since its initial upload in 2020, suggesting a stable but not actively developed state.","status":"maintenance","version":"0.1.0","language":"en","source_language":"en","source_url":"https://github.com/Melevir/flake8-cognitive-complexity","tags":["flake8","linter","code quality","complexity","cognitive complexity","static analysis"],"install":[{"cmd":"pip install flake8-cognitive-complexity","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"This is a plugin for Flake8, requiring a compatible Flake8 installation to function.","package":"flake8"},{"reason":"Requires Python >=3.6.","package":"python","optional":false},{"reason":"This package is a plugin wrapper for the underlying 'cognitive-complexity' library, which provides the core logic for calculating cognitive complexity.","package":"cognitive-complexity","optional":false}],"imports":[],"quickstart":{"code":"# test_code.py\ndef f(a, b):\n    if a:\n        for i in range(b):\n            if b:\n                return 1\n\n# Save the above as test_code.py\n\n# To run from terminal (after 'pip install flake8 flake8-cognitive-complexity'):\n# flake8 test_code.py\n# flake8 --max-cognitive-complexity=3 test_code.py\n","lang":"python","description":"After installation, `flake8-cognitive-complexity` automatically integrates with `flake8`. Simply run `flake8` on your project. By default, it uses a cognitive complexity threshold of 7. You can configure this maximum allowed cognitive complexity using the `--max-cognitive-complexity` option either on the command line or in your `flake8` configuration file (e.g., `setup.cfg` or `tox.ini`)."},"warnings":[{"fix":"Check the GitHub repository for recent commits or open issues. If encountering compatibility problems with newer environments, consider the project's maintenance status or community forks.","message":"The library has had only one release (0.1.0) since July 2020. This suggests infrequent updates and potentially unaddressed issues or lack of active development. Consider its long-term support and compatibility with newer Python and Flake8 versions.","severity":"gotcha","affected_versions":"0.1.0"},{"fix":"Adjust the threshold using `--max-cognitive-complexity=<value>` in your `flake8` command or configuration file (e.g., `setup.cfg`, `tox.ini`) under the `[flake8]` section. For instance: `[flake8]\nmax-cognitive-complexity = 15`.","message":"The default cognitive complexity threshold is 7. Projects with existing complex codebases might generate numerous warnings upon initial integration. This may require a higher threshold initially or gradual refactoring to meet the default.","severity":"gotcha","affected_versions":"0.1.0"},{"fix":"If encountering unexpected errors or plugin not loading, try locking your `flake8` version to one known to be compatible (e.g., `flake8<4.0`). Refer to `flake8`'s release notes for breaking changes related to its plugin interface.","message":"As a `flake8` plugin, `flake8-cognitive-complexity`'s compatibility is tied to the `flake8` version. The README notes testing on `flake8 3.7.8`. Newer `flake8` versions might introduce subtle incompatibilities with older plugins.","severity":"gotcha","affected_versions":"0.1.0 (with various flake8 versions)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Refactor the function to reduce its complexity. This can involve splitting it into smaller functions, reducing nesting levels, simplifying conditional logic, or extracting helper methods. Alternatively, increase the `max-cognitive-complexity` threshold in your `flake8` configuration.","cause":"A function's cognitive complexity calculation exceeds the configured or default maximum threshold (default 7).","error":"CCR001 Cognitive complexity is too high (X > Y)"},{"fix":"Install `flake8` in your current Python environment using `pip install flake8`. If using a virtual environment, ensure it is activated before running `flake8`.","cause":"The `flake8` executable is not found in the system's PATH, typically because it's not installed or the virtual environment where it's installed is not active.","error":"flake8: command not found"},{"fix":"Verify that `pip install flake8-cognitive-complexity` was executed in the same Python environment that `flake8` is running from. You can check if the plugin is detected by running `flake8 --version` and looking for 'cognitive-complexity' in the output. If it's not there, reinstall the plugin or check your environment setup.","cause":"The `flake8-cognitive-complexity` plugin is not correctly installed, or `flake8` cannot discover it in the current Python environment.","error":"Error: No such option: --max-cognitive-complexity"}]}