Skip to main content

CRAP score per function for Python source files

Project description

crap4py

CI PyPI

Python port of Uncle Bob's crap4go and crap4clj — CRAP score per function for Python source files.

CRAP (Change Risk Anti-Pattern) = CC² × (1 − coverage)³ + CC — a per-function metric combining cyclomatic complexity and branch coverage. Higher means riskier to change. Conventional threshold: 30.

Install

# Run without installing
uvx crap4py

# Persistent install
uv tool install crap4py

Usage

crap4py src/ --lcov lcov.info

Generate lcov.info with coverage.py:

pytest --cov --cov-branch --cov-report=lcov:lcov.info

Output

CRAP Report
Function                    | Module                        | CC | Cov%  | CRAP
----------------------------+-------------------------------+----+-------+-----
_sort_key                   | src/crap4py/_crap.py          |  3 |  33.3 |  9.9
main                        | src/crap4py/__main__.py       |  2 | 100.0 |  2.0

Options

Flag Description
--lcov PATH LCOV branch-coverage file (required)
--max-crap N Exit non-zero if any function exceeds N (CI gate)
--max-workers N Parallel workers for large codebases
--fragment TEXT Only analyse files whose path contains TEXT

CI gate

crap4py src/ --lcov lcov.info --max-crap 30

Exits non-zero if any function's CRAP score exceeds the threshold. N/A functions (no coverage data) never trip the gate.

How it works

  • Complexity — cyclomatic complexity from Python's ast per def/async def
  • Coverage — branch coverage from LCOV BRDA records intersected with each function's line range
  • Skips.gitignore-ignored paths and test files (test_*.py, *_test.py) are excluded automatically

See also: crap4go, crap4clj.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crap4py-0.1.1.tar.gz (216.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crap4py-0.1.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file crap4py-0.1.1.tar.gz.

File metadata

  • Download URL: crap4py-0.1.1.tar.gz
  • Upload date:
  • Size: 216.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for crap4py-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bd5f4ecc1613490515fd229f6958fad06cf448497e6c99a612712fce965272cc
MD5 61942fc910278c138c9cf8600adaf3bb
BLAKE2b-256 48a8dc6cc095b29eeba5c37ab04605819782f5f5e080cde38c97063bbeff1780

See more details on using hashes here.

Provenance

The following attestation bundles were made for crap4py-0.1.1.tar.gz:

Publisher: release.yml on gabadi/crap4py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file crap4py-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: crap4py-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for crap4py-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2cdaf28dccfc88313c95f1bacdf99fe19a39efbe9855d981352bb02a7a17a90c
MD5 5ca94de8e3aa1fc6f1abc21c630a0d69
BLAKE2b-256 03c6eda37aa64d84a3e22a5c9242e5696a83750c7737dc99d43bce97dfc53e53

See more details on using hashes here.

Provenance

The following attestation bundles were made for crap4py-0.1.1-py3-none-any.whl:

Publisher: release.yml on gabadi/crap4py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page