Skip to main content

congnitive-complexity

Build Status Maintainability Test Coverage PyPI version PyPI - Python Version

Library to calculate Python functions cognitive complexity via code.

Installation

pip install cognitive_complexity

Usage

>>> import ast

>>> funcdef = ast.parse("""
... def f(a):
...     return a * f(a - 1)  # +1 for recursion
... """).body[0]

>>> from cognitive_complexity.api import get_cognitive_complexity
>>> get_cognitive_complexity(funcdef)
1

Flake8-Cognitive-Complexity Extension

Perhaps the most common way to use this library (especially if you are already using the Flake8 linter) is to use the flake8-cognitive-complexity extension. If you run Flake8 with this extension installed, Flake8 will let you know if your code is too complex. For more details and documentation, visit the flake8-cognitive-complexity extension repository.

What is cognitive complexity

Here are some readings about cognitive complexity:

Realization details

This is not precise realization of original algorithm proposed by G. Ann Campbell, but it gives rather similar results. The algorithm gives complexity points for breaking control flow, nesting, recursion, stacks logic operation etc.

Contributing

We would love you to contribute to our project. It's simple:

  • Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
  • Create a pull request. Make sure all checks are green.
  • Fix review comments if any.
  • Be awesome.

Here are useful tips:

  • You can run all checks and tests with make check. Please do it before TravisCI does.
  • We use BestDoctor python styleguide. Sorry, styleguide is available only in Russian for now.
  • We respect Django CoC. Make soft, not bullshit.

Metadata

Release files for cognitive-complexity 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cognitive-complexity 1.3.0
File Size Uploaded
cognitive_complexity-1.3.0.tar.gz 5.7 kB Details

Release files / cognitive_complexity-1.3.0.tar.gz

Download URL cognitive_complexity-1.3.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a0cfbd47dee0b19f4056f892389f501694b205c3af69fb703cc744541e03dde5
BLAKE2b-256 checksum
How to use checksums
6aff3cd46792fcbf742458083527407bc336efe382b168595583a06c70bf8e54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release history Release notifications | RSS feed

This release

1.3.0 This release

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page