skillfed

cognitive-complexity

Library to calculate Python functions cognitive complexity via code

cognitive-complexity v1.3.0 352.8K downloads/30d#7,308 on PyPI47
Permissive license MIT Abandoned released

What it is and what it does

Cognitive-complexity is a Python library that computes a numeric score representing how cognitively difficult a function is to understand. It works by parsing Python source code into an abstract syntax tree and applying heuristics that award complexity points for control-flow breaks, nesting depth, recursion, and logical operations. The library itself has no runtime dependencies and is typically used either as a standalone API or integrated into flake8 via the flake8-cognitive-complexity extension.

The package is based on research into cognitive complexity as a measure of code understandability—distinct from traditional cyclomatic complexity. The implementation is acknowledged to be an approximation rather than a precise realization of the original algorithm. The project has been abandoned and receives no active maintenance, though it remains functional for Python 3.6+ codebases.

Use it for:

  • Integrate into a CI/CD pipeline to flag functions exceeding a complexity threshold and enforce code review for high-complexity changes.
  • Use as a flake8 plugin to get real-time warnings during development when functions become too difficult to understand.
  • Analyze legacy codebases to identify which functions are candidates for refactoring based on cognitive load.
  • Build custom linting rules that combine cognitive complexity with other metrics to guide architectural decisions.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Calculates cognitive complexity scores for Python functions by analyzing their abstract syntax tree, measuring how difficult code is to understand based on control flow, nesting, recursion, and logical operations.

Yes, if you need cognitive complexity analysis and are comfortable with an unmaintained library. The package is stable, has no dependencies, and works reliably on Python 3.6+. However, consider it a read-only tool: expect no bug fixes, security patches, or feature updates. If active maintenance is critical for your workflow, investigate whether a maintained alternative exists before committing to this version.

Install

cognitive-complexity on PyPI

pip

pip install cognitive-complexity

uv

uv add cognitive-complexity

poetry

poetry add cognitive-complexity

Installing cognitive-complexity

Before you install

High install friction: no runtime dependencies but marked as abandoned since the last commit on 2022-08-09 with no maintenance or security updates.

License in practice

MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.

Quickstart

import ast
from cognitive_complexity.api import get_cognitive_complexity

funcdef = ast.parse("def f(a):\n    return a * f(a - 1)").body[0]
print(get_cognitive_complexity(funcdef))  # Output: 1

Requires Python 3.6 or later; input must be an AST node (typically from ast.parse), not raw source code.

Verify before relying

  • How closely the implementation matches G. Ann Campbell's original algorithm and whether deviations affect reliability for production linting.
  • Whether the package is maintained under a different repository or if a maintained fork exists as a drop-in replacement.
  • Current stability and compatibility with Python versions beyond 3.8 given the abandoned maintenance status.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.6)
Install friction high — source build required
Runtime dependencies none
Maintenance abandoned — 1,466 days since the last release
Last repo commit
First released
Downloads 352,807/month — #7,308 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: cognitive_complexity-1.3.0.tar.gz

Keywords: flake8

Environment :: ConsoleOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Topic :: Software Development :: DocumentationTopic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: Quality Assurance

Tags

cognitive complexity measurementcode complexity analyzer pythonfunction complexity scoringcode readability metricsast-based complexity calculationmaintainability analysis toolflake8 complexity plugin
code-qualityast-analysislinting

More Python Modules packages