Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork12
Blazingly fast cognitive complexity analysis for Python, written in Rust.
License
rohaquinlop/complexipy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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
pip install complexipy# oruv add complexipy# Analyze current directorycomplexipy.# Analyze specific file/directorycomplexipy path/to/code.py# Analyze with custom thresholdcomplexipy. --max-complexity-allowed 10# Save results to JSON/CSVcomplexipy. --output-json --output-csv# Analyze current directory while excluding specific filescomplexipy. --exclude path/to/exclude.py --exclude path/to/other/exclude.py
fromcomplexipyimportfile_complexity,code_complexity# Analyze a fileresult=file_complexity("app.py")print(f"File complexity:{result.complexity}")forfuncinresult.functions:print(f"{func.name}:{func.complexity}")# Analyze code stringsnippet="""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}")
🔧 GitHub Actions
-uses:rohaquinlop/complexipy-action@v2with:paths:.max_complexity_allowed:10output_json:true
🪝 Pre-commit Hook
repos:-repo:https://github.com/rohaquinlop/complexipy-pre-commitrev:v3.0.0hooks: -id:complexipy
🔌 VS Code Extension
Install from themarketplace for real-time complexity analysis with visual indicators.
complexipy supports configuration via TOML files. Configuration files are loaded in this order of precedence:
complexipy.toml(project-specific config).complexipy.toml(hidden config file)pyproject.toml(under[tool.complexipy]section)
# complexipy.toml or .complexipy.tomlpaths = ["src","tests"]max-complexity-allowed =10quiet =falseignore-complexity =falsedetails ="normal"color ="auto"sort ="asc"exclude = [][output]csv =truejson =true
# pyproject.toml[tool.complexipy]paths = ["src","tests"]max-complexity-allowed =10quiet =falseignore-complexity =falsedetails ="normal"color ="auto"sort ="asc"exclude = [][tool.complexipy.output]csv =truejson =true
| Flag | Description | Default |
|---|---|---|
--exclude | Exclude entries relative to each provided path. Entries resolve to existing directories (prefix match) or files (exact match). Non-existent entries are ignored. | |
--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 |
--color <auto|yes|no> | Use color | auto |
--sort <asc|desc|name> | Sort results | asc |
--quiet | Suppress output | false |
--ignore-complexity | Don't exit with error on threshold breach | false |
--version | Show installed complexipy version and exit | - |
Example:
# Exclude only top-level 'tests' directory under the provided rootcomplexipy . --exclude tests# This will not exclude './complexipy/utils.py' if you pass '--exclude utils' at repo root,# because there is no './utils' directory or file at that level.You can explicitly ignore a known complex function inline, similar to Ruff'sC901 ignores:
deflegacy_adapter(x,y):# noqa: complexipy (safe wrapper)ifxandy:returnx+yreturn0
Place# noqa: complexipy on the function definition line (or the line immediately above). An optional reason can be provided in parentheses or plain text, it’s ignored by the parser.
# Core functionsfile_complexity(path:str)->FileComplexitycode_complexity(source:str)->CodeComplexity# Return typesFileComplexity: ├─path:str ├─complexity:int └─functions:List[FunctionComplexity]FunctionComplexity: ├─name:str ├─complexity:int ├─line_start:int └─line_end:int
Inspired by theCognitive Complexity research by SonarSource, G. Ann Campbell
Built with ❤️ by@rohaquinlop andcontributors
About
Blazingly fast cognitive complexity analysis for Python, written in Rust.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.