Skip to main content

pylint-cognitive-complexity

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

Install

Install it into the environment that runs pylint, for example:

uv add --dev pylint-cognitive-complexity

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.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 pylint-cognitive-complexity 0.1.0
File Size Uploaded Provenance Actions
pylint_cognitive_complexity-0.1.0.tar.gz 4.5 kB Without provenance Details for pylint_cognitive_complexity-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 10.3 kB

Release files / pylint_cognitive_complexity-0.1.0.tar.gz

Download URL pylint_cognitive_complexity-0.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
88c602c967b616aaf69df95fc048e15f49e4b99bdc995d8c6410ca9f399e5c6f
BLAKE2b-256 checksum
How to use checksums
5909a519e28324be5f4d35082451e61898553687413d14fb00076be3d5be2007
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.0-py3-none-any.whl

Download URL pylint_cognitive_complexity-0.1.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38850968caef70f6aa6bfcb1434bb82ec3c1eec60b5c22962421708d53b2ec5e
BLAKE2b-256 checksum
How to use checksums
8a50a6fb83e462cff479c95c37e5a11d7e6ff382169efb493a798d2404fd464c
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.0

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