Registry / testing / complexipy

complexipy

JSON →
library5.5.0pypypi✓ verified 87d ago

Complexipy is an extremely fast Python library, written in Rust, designed to calculate the cognitive complexity of Python files. Unlike cyclomatic complexity, it focuses on measuring how difficult code is for humans to read and understand, identifying hard-to-maintain sections. It operates as both a command-line interface (CLI) tool and a Python API, facilitating integration into development workflows, CI/CD pipelines, and pre-commit hooks. The current version is 5.2.0, with an active release cadence, frequently adding new features and improvements.

pip install complexipy
INSTALL
IMPORT
SIG · COMPLEXIPY
C
complexipy
testingpythonv5.5.0
Install
3.2s avg
Import
15ms
Disk
38MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.5.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
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.2s · import 0.015s · 39MB
38MB installed
● package 38MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

file_complexity
from complexipy import file_complexity
code_complexity
from complexipy import code_complexity

This quickstart demonstrates how to use `complexipy` both by analyzing a Python code string directly and by analyzing a Python file on disk. It imports `file_complexity` and `code_complexity` to get detailed cognitive complexity metrics for functions within the given code.

import os from complexipy import file_complexity, code_complexity # Example 1: Analyze a Python string snippet = """ def calculate_discount(price, quantity, is_member): if is_member: if quantity > 10: return price * quantity * 0.8 # 20% discount else: return price * quantity * 0.9 # 10% discount else: return price * quantity """ print("\n--- Analyzing code snippet ---") result_code = code_complexity(snippet) print(f"Overall snippet complexity: {result_code.complexity}") for func in result_code.functions: print(f" Function '{func.name}': Complexity {func.complexity}, Line {func.line_start}-{func.line_end}") # Example 2: Analyze a temporary Python file file_content = """ def process_data(data): if data.get('status') == 'active': for item in data.get('items', []): if item.get('value', 0) > 100: print(f"Processing high-value item: {item['id']}") else: print(f"Processing regular item: {item['id']}") elif data.get('status') == 'pending': print("Data pending, skipping processing.") else: print("Unknown data status.") """ file_path = "example_module.py" with open(file_path, "w") as f: f.write(file_content) print(f"\n--- Analyzing file: {file_path} ---") result_file = file_complexity(file_path) print(f"File complexity: {result_file.complexity}") for func in result_file.functions: print(f" Function '{func.name}': Complexity {func.complexity}, Line {func.line_start}-{func.line_end}") # Clean up temporary file os.remove(file_path)
complexipy --version
Debug
Known issues
breakingIn version 4.0.0, the calculation method for boolean operators in conditions was updated to align with the original Cognitive Complexity paper. This change may result in higher reported complexity scores for existing functions, potentially causing previously passing checks to fail.
fix
Review existing codebases and adjust complexity thresholds (`--max-complexity-allowed`) or refactor high-complexity functions. Consider using `--snapshot-create` (v5.0.0+) to establish a baseline for existing complexity.
affects: >=4.0.0
breakingVersion 5.0.0 introduced a significant change where conditional scoring now counts each `elif` and `else` branch as an additional +1 complexity (plus its boolean test), aligning with Sonar's cognitive complexity rules. This will likely lead to higher scores for functions with multiple branching statements.
fix
Be aware that complexity scores for functions with extensive conditional logic might increase. Update your `pyproject.toml` configuration or CLI flags to reflect new acceptable thresholds if necessary. Refactor deeply nested or highly branched logic.
affects: >=5.0.0
gotchaStarting from version 5.0.0, `complexipy` introduced snapshot baselines (`--snapshot-create`) and per-target change tracking with a `.complexipy_cache` directory. If a `complexipy-snapshot.json` file exists, the tool will automatically fail CI if new functions exceed the threshold or tracked functions become more complex. It will also update the snapshot on improvements.
fix
Understand the snapshot behavior for CI/CD. Use `--snapshot-ignore` to bypass snapshot comparisons temporarily or review and commit updated snapshots (`complexipy-snapshot.json`) when improvements are made. Manage `.complexipy_cache` if you need to clear old change tracking data.
affects: >=5.0.0
deprecatedWith the release of version 5.0.0, legacy TOML configuration keys (e.g., `output-json = true`) and CLI flags (e.g., `--output-json`) for output format are deprecated. They are replaced by the unified `output-format` key/flag which accepts values like `json` or `csv`.
fix
Update your `pyproject.toml` or `complexipy.toml` configuration and CLI commands to use `output-format = "json"` or `--output-format json` respectively, for generating reports in specific formats.
affects: >=5.0.0
Errors
Common errors & fixes
Command 'complexipy' not found
The `complexipy` command-line tool is not installed or is not accessible in your system's PATH. This usually happens if the package wasn't installed correctly or the Python environment is not activated.
fix
Ensure `complexipy` is installed with `pip install complexipy`. If using a virtual environment, activate it before running the command. Verify the Python scripts directory is in your system's PATH.
CLI exits with code 1 (or fails CI pipeline) when complexity exceeds threshold
By default, `complexipy` is configured to exit with a non-zero status code (indicating failure) if any analyzed function's cognitive complexity exceeds the `--max-complexity-allowed` threshold (defaulting to 15). This behavior is intended for enforcing code quality.
fix
To prevent failure on high complexity, use `complexipy . --ignore-complexity` to report all functions without failing, or set a higher threshold with `complexipy . --max-complexity-allowed <value>`. To disable the error exit entirely, use `complexipy . --max-complexity-allowed 0`.
error: path 'non_existent_file.py' does not exist (or similar file/directory not found error)
The file or directory path provided to the `complexipy` command does not exist at the specified location, or there's a typo in the path.
fix
Double-check the path provided to `complexipy`. Ensure it is correct and accessible from your current working directory. Use absolute paths for clarity or navigate to the correct directory before execution.
Upgrade
Version history
5.5.0latest on PyPI · released May 22, 2026
Audit
Dependencies
gitoptionalRequired for analyzing remote Git repositories or tracking complexity diffs against Git references.
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources