Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

rohaquinlop/complexipy

Repository files navigation

complexipy

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

PyPIDownloadsLicense

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# oruv add complexipy

Quick Start

Command Line

# 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

Python API

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}")

Integrations

🔧 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.

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

CLI Options

FlagDescriptionDefault
--excludeExclude entries relative to each provided path. Entries resolve to existing directories (prefix match) or files (exact match). Non-existent entries are ignored.
--max-complexity-allowedComplexity threshold15
--output-jsonSave results as JSONfalse
--output-csvSave results as CSVfalse
--details <normal|low>Output verbositynormal
--color <auto|yes|no>Use colorauto
--sort <asc|desc|name>Sort resultsasc
--quietSuppress outputfalse
--ignore-complexityDon't exit with error on threshold breachfalse
--versionShow 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.

Inline Ignores

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.

API Reference

# 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

DocumentationPyPIGitHub

Built with ❤️ by@rohaquinlop andcontributors

About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp