Skip to main content

pylint-cognitive-complexity

A pylint plugin that reports functions and methods whose cognitive complexity is above a configurable maximum.

Installation

Requires Python 3.10+ and pylint 4.1.1+.

With pip:

pip install pylint-cognitive-complexity

With uv, as a dev dependency of the project that runs pylint:

uv add --dev pylint-cognitive-complexity

Or from a checkout:

git clone https://github.com/eclypsium/pylint-cognitive-complexity.git
cd pylint-cognitive-complexity
pip install -e .        # or: uv pip install -e .

Usage

pylint --load-plugins=pylint_cognitive_complexity mypackage

or in pyproject.toml:

[tool.pylint.main]
load-plugins = ["pylint_cognitive_complexity"]

[tool.pylint.cognitive-complexity]
max-cognitive-complexity = 4
Message Symbol Meaning
R7001 too-cognitively-complex The function scores above max-cognitive-complexity.
Option Default Meaning
max-cognitive-complexity 4 Highest score that passes.

Silence a single function with # pylint: disable=too-cognitively-complex on its def line.

Scoring

The rules follow the SonarSource specification as SonarPython applies it to Python. n is the nesting level, which starts at 0 in a function body.

Construct Adds Nesting inside it
if, ternary x if c else y 1 + n n + 1
elif 1 n + 1
else on if, for, while or try 1 n + 1
for, async for, while 1 + n n + 1
each except or except* clause 1 + n n + 1
match 1 + n n + 1 in each case
each sequence of like boolean operators 1 n
nested def, async def, lambda 0 n + 1
try body, finally, with, class, comprehensions, break, continue 0 n

So a and b and c adds 1, while a and b or c adds 2.

A nested function counts toward the function that contains it, and is not reported on its own. Recursion is not counted.

Comprehensions

A comprehension's for and if clauses add nothing and do not nest. A comprehension is a declarative shorthand for a loop, so it reads as one step. Cyclomatic complexity, as radon measures it, charges 1 per for plus 1 per if. That lets a few readable comprehensions push a straight-line function out of radon's rank A. Boolean operators or ternaries written inside a comprehension still count, the same as anywhere else.

Why the default is 4

The default is meant to pass the same functions as radon's rank A (4), that is a cyclomatic complexity of at most 4. The two metrics have no exact conversion, so the threshold was calibrated by measurement.

Both metrics were computed for every function in the CPython standard library and a large set of third-party packages. Each threshold was then scored on how often "cognitive complexity ≤ T" agreed with "radon CC ≤ 4". T = 4 gave the highest agreement, and it also flagged about the same share of functions as radon does. T = 4 still came out best when radon's comprehension increments were subtracted first.

To repeat the measurement, run:

uv run scripts/calibrate.py [DIR ...]

With no directories, it measures the running interpreter's standard library and site-packages. For each threshold it prints the share of functions flagged, the agreement with radon, and Cohen's kappa, and it marks the best threshold.

Metadata

Release files for pylint-cognitive-complexity 0.1.1

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

Source distribution (sdist)

Source distribution for pylint-cognitive-complexity 0.1.1
File Size Uploaded Provenance Actions
pylint_cognitive_complexity-0.1.1.tar.gz 4.6 kB Without provenance Details for pylint_cognitive_complexity-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pylint-cognitive-complexity 0.1.1
File Interpreter ABI Platform Actions
pylint_cognitive_complexity-0.1.1-py3-none-any.whl Python 3 none any Details for pylint_cognitive_complexity-0.1.1-py3-none-any.whl

Total release size: 10.5 kB

Release files / pylint_cognitive_complexity-0.1.1.tar.gz

Download URL pylint_cognitive_complexity-0.1.1.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1bd1a248bb9a06b78c0f19c1bca9961e09a4634f601cb4a35c83e57dbeaca7e2
BLAKE2b-256 checksum
How to use checksums
42989a631e080c37f42c4a325cf0c8c61aeb9db3d74d35e12b62785f4bc45785
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pylint_cognitive_complexity-0.1.1-py3-none-any.whl

Download URL pylint_cognitive_complexity-0.1.1-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc1a036783ce699009908e0805a80058d58a4464e377dd74e501c7e2eadda303
BLAKE2b-256 checksum
How to use checksums
ccdc4bfe10fb602a5eea1f0f97167ee992580c91346c1a566f9288ea13f43773
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for pylint-cognitive-complexity 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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