Registry / testing / flake8-cognitive-complexity

flake8-cognitive-complexity

JSON →
library0.1.0pypypiunverified

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.

pip install flake8-cognitive-complexity
INSTALL
IMPORT
SIG · FLAKE8-COGNITIVE-C
F
flake8-cognitive-complexity
testingpythonv0.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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`).

# test_code.py def f(a, b): if a: for i in range(b): if b: return 1 # Save the above as test_code.py # To run from terminal (after 'pip install flake8 flake8-cognitive-complexity'): # flake8 test_code.py # flake8 --max-cognitive-complexity=3 test_code.py
Debug
Known issues
gotchaThe 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.
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.
affects: 0.1.0
gotchaThe 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.
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]
max-cognitive-complexity = 15`.
affects: 0.1.0
gotchaAs 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.
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.
affects: 0.1.0 (with various flake8 versions)
Errors
Common errors & fixes
CCR001 Cognitive complexity is too high (X > Y)
A function's cognitive complexity calculation exceeds the configured or default maximum threshold (default 7).
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.
flake8: command not found
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.
fix
Install `flake8` in your current Python environment using `pip install flake8`. If using a virtual environment, ensure it is activated before running `flake8`.
Error: No such option: --max-cognitive-complexity
The `flake8-cognitive-complexity` plugin is not correctly installed, or `flake8` cannot discover it in the current Python environment.
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.
Upgrade
Version history
0.1.0latest on PyPI · released Aug 1, 2020
Audit
Dependencies
flake8requiredThis is a plugin for Flake8, requiring a compatible Flake8 installation to function.
pythonrequiredRequires Python >=3.6.
cognitive-complexityrequiredThis package is a plugin wrapper for the underlying 'cognitive-complexity' library, which provides the core logic for calculating cognitive complexity.
Agent activity
2 hits · last 30 days
node
2
Resources
flake8-cognitive-complexity — pip install flake8-cognitive-complexity · libregistry