Skip to content

rohaquinlop/complexipy

Repository files navigation

complexipy

complexipy

Blazingly fast cognitive complexity analysis for Python, written in Rust.

PyPI Downloads License

InstallationQuick StartIntegrationsDocumentation

What is Cognitive Complexity?

Cognitive complexity measures how hard code is to understand by humans, not machines.

Unlike traditional metrics, cognitive complexity considers the mental effort required to read and comprehend code. It identifies truly complex code that needs refactoring, making it perfect for code reviews and maintaining clean codebases.

Key benefits:

  • Human-focused — Aligns with developer intuition
  • Actionable insights — Pinpoints hard-to-understand code
  • Better maintenance — Improves long-term code quality

Installation

pip install complexipy
# or
uv add complexipy

Quick Start

Command Line

# Analyze current directory
complexipy .

# Analyze specific file/directory
complexipy path/to/code.py

# Analyze with custom threshold
complexipy . --max-complexity-allowed 10

# Save results to JSON/CSV
complexipy . --output-json --output-csv

Python API

from complexipy import file_complexity, code_complexity

# Analyze a file
result = file_complexity("app.py")
print(f"File complexity: {result.complexity}")

for func in result.functions:
    print(f"{func.name}: {func.complexity}")

# Analyze code string
snippet = """
def complex_function(data):
    if data:
        for item in data:
            if item.is_valid():
                process(item)
"""

result = code_complexity(snippet)
print(f"Complexity: {result.complexity}")

Integrations

🔧 GitHub Actions
- uses: rohaquinlop/complexipy-action@v2
  with:
    paths: .
    max_complexity_allowed: 10
    output_json: true
🪝 Pre-commit Hook
repos:
- repo: https://github.com/rohaquinlop/complexipy-pre-commit
  rev: v3.0.0
  hooks:
    - id: complexipy
🔌 VS Code Extension

Install from the marketplace for real-time complexity analysis with visual indicators.

Configuration

TOML Configuration Files

complexipy supports configuration via TOML files. Configuration files are loaded in this order of precedence:

  1. complexipy.toml (project-specific config)
  2. .complexipy.toml (hidden config file)
  3. pyproject.toml (under [tool.complexipy] section)

Example Configuration

# complexipy.toml or .complexipy.toml
paths = ["src", "tests"]
max-complexity-allowed = 10
quiet = false
ignore-complexity = false
details = "normal"
sort = "asc"

[output]
csv = true
json = true
# pyproject.toml
[tool.complexipy]
paths = ["src", "tests"]
max-complexity-allowed = 10
quiet = false
ignore-complexity = false
details = "normal"
sort = "asc"

[tool.complexipy.output]
csv = true
json = true

CLI Options

Flag Description Default
--max-complexity-allowed Complexity threshold 15
--output-json Save results as JSON false
--output-csv Save results as CSV false
--details <normal|low> Output verbosity normal
--sort <asc|desc|name> Sort results asc
--quiet Suppress output false
--ignore-complexity Don't exit with error on threshold breach false

API Reference

# Core functions
file_complexity(path: str) -> FileComplexity
code_complexity(source: str) -> CodeComplexity

# Return types
FileComplexity:
  ├─ path: str
  ├─ complexity: int  
  └─ functions: List[FunctionComplexity]

FunctionComplexity:
  ├─ name: str
  ├─ complexity: int
  ├─ line_start: int
  └─ line_end: int

DocumentationPyPIGitHub

Built with ❤️ by @rohaquinlop and contributors

Inspired by the Cognitive Complexity research by SonarSource

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •