An extremely fast Python library to calculate the cognitive complexity of python files, written in Rust.
Documentation: https://rohaquinlop.github.io/complexipy/
Source Code: https://github.com/rohaquinlop/complexipy
PyPI: https://pypi.org/project/complexipy/
- Python >= 3.11
- You also need to install
git
in your computer if you want to analyze a git repository.
pip install complexipy
To run complexipy you can use the following command:
complexipy . # Use complexipy to analyze the current directory and any subdirectories
complexipy path/to/directory # Use complexipy to analyze a specific directory and any subdirectories
complexipy git_repository_url # Use complexipy to analyze a git repository
complexipy path/to/file.py # Use complexipy to analyze a specific file
complexipy path/to/file.py -c 20 # Use the -c option to set the maximum congnitive complexity, default is 15
complexipy path/to/directory -c 0 # Set the maximum cognitive complexity to 0 to disable the exit with error
complexipy path/to/directory -o # Use the -o option to output the results to a CSV file, default is False
complexipy path/to/directory -d low # Use the -d option to set detail level, default is "normal". If set to "low" will show only files with complexity greater than the maximum complexity
If the cognitive complexity of a file is greater than the maximum cognitive, then the return code will be 1 and exit with error, otherwise it will be 0.
For example, given the following file:
def a_decorator(a, b):
def inner(func):
return func
return inner
def b_decorator(a, b):
def inner(func):
if func:
return None
return func
return inner
The cognitive complexity of the file is 1, and the output of the command
complexipy path/to/file.py
will be:
─────────────────────── complexipy 0.2.2 🐙 ───────────────────────
- Finished analysis in test_decorator.py
──────────────────── 🎉 Analysis completed!🎉 ─────────────────────
Summary
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Path ┃ File ┃ Complexity ┃
┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ test_decorator.py │ test_decorator.py │ 1 │
└───────────────────┴───────────────────┴────────────┘
🧠 Total Cognitive Complexity in ./tests/test_decorator.py: 1
1 files analyzed in 0.0005 seconds
If you want to output the results to a CSV file, you can use the -o
option,
this is really useful if you want to integrate complexipy with other tools,
for example, a CI/CD pipeline. You will get the output in the console and will
create a CSV file with the results of the analysis.
The filename will be complexipy.csv
and will be saved in the current directory.
$ complexipy path/to/file.py -o
The output will be:
Path,File Name,Cognitive Complexity
test_decorator.py,test_decorator.py,1
You can also analyze a directory, for example:
$ complexipy .
And complexipy will analyze all the files in the current directory and any subdirectories.
You can also analyze a git repository, for example:
$ complexipy https://github.com/rohaquinlop/complexipy
And to generate the output to a CSV file:
$ complexipy https://github.com/rohaquinlop/complexipy -o
This project is licensed under the MIT License - see the LICENSE file for details.
- Thanks to G. Ann Campbell for publishing the paper "Cognitive Complexity a new way to measure understandability".
- This project is inspired by the Sonar way to calculate the cognitive complexity.