riskratchet provides a mechanical way to enforce maintainability in Python code, analyzing function risk scores based on coverage gaps and complexity. It improves code quality by failing CI for risky changes and preventing complexity creep, enabling developers to focus on building robust applications without the 'complexity cop' burden.

Riskratchet is a powerful tool designed to enhance the maintainability of Python code by effectively managing risks associated with code complexity and testing. It systematically computes a per-function risk score based on several factors such as coverage gaps, cyclomatic complexity, and churn, ensuring that the risk level never increases beyond a predefined baseline.
AI coding agents excel at generating code that is functional; however, they often fall short in the following critical areas:
Riskratchet addresses these challenges by serving as an automated safeguard, computing risk scores that highlight potential issues while allowing teams to maintain their focus on development.
Install Riskratchet (Use pip or run without installation):
pip install riskratchet
# or run without installing
uvx riskratchet --help
Run Tests with Coverage to generate a JSON report:
pytest --cov --cov-report=json:coverage.json
Create a Baseline that defines the current acceptable risk profile:
riskratchet baseline src --coverage coverage.json --output .riskratchet.json
Scan Code for risks based on the established profile:
riskratchet scan src --coverage coverage.json
Check for Regressions after a commit:
riskratchet check src --coverage coverage.json --baseline .riskratchet.json
The check command returns 1 for regressions, 2 for usage errors, and 0 for a successful run.
Riskratchet offers seamless integration as a GitHub Action, making it easy to include in pull request workflows. It automatically checks for quality violations and provides insightful comments directly on PRs to inform contributors of any issues:
# .github/workflows/riskratchet.yml
on: [pull_request]
jobs:
riskratchet:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: KayhanB21/riskratchet@v0.2.13
with:
coverage: coverage.json
The Riskratchet scoring system evaluates each function based on six components:
| Component | Weight | Description |
|---|---|---|
| Coverage Gap | 30% | Measures the percentage of untested lines in a function. |
| Structural Complexity | 25% | Evaluates the cyclomatic complexity of the function. |
| Branch Gap | 15% | Assesses the percentage of untested branches. |
| Churn | 10% | Counts recent changes to a function within a specified timeframe. |
| Public Surface | 10% | Considers the impact of public functions on overall risk. |
| Sprawl | 10% | Analyzes function and file length relative to others. |
Riskratchet is an essential tool for Python developers aiming to maintain a high standard of code quality while leveraging AI-assisted coding. By effectively identifying and managing risks associated with complexity and testing, it empowers teams to focus on innovation rather than administrative oversight. Explore more on the PyPI project page and start improving code maintainability today.
No comments yet.
Sign in to be the first to comment.