python-code-quality 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_code_quality-0.1.4/.github/workflows/python-publish.yml +70 -0
- python_code_quality-0.1.4/.gitignore +17 -0
- python_code_quality-0.1.4/.python-version +1 -0
- python_code_quality-0.1.4/CLAUDE.md +54 -0
- python_code_quality-0.1.4/LICENSE +21 -0
- python_code_quality-0.1.4/PKG-INFO +188 -0
- python_code_quality-0.1.4/README.md +162 -0
- python_code_quality-0.1.4/data/problems/travelling_salesman/ts_bad.py +65 -0
- python_code_quality-0.1.4/data/problems/travelling_salesman/ts_good.py +61 -0
- python_code_quality-0.1.4/pyproject.toml +39 -0
- python_code_quality-0.1.4/src/py_cq/__init__.py +10 -0
- python_code_quality-0.1.4/src/py_cq/cli.py +229 -0
- python_code_quality-0.1.4/src/py_cq/config/__init__.py +27 -0
- python_code_quality-0.1.4/src/py_cq/config/tools.yaml +97 -0
- python_code_quality-0.1.4/src/py_cq/context_hash.py +81 -0
- python_code_quality-0.1.4/src/py_cq/execution_engine.py +160 -0
- python_code_quality-0.1.4/src/py_cq/llm_formatter.py +47 -0
- python_code_quality-0.1.4/src/py_cq/localtypes.py +135 -0
- python_code_quality-0.1.4/src/py_cq/main.py +12 -0
- python_code_quality-0.1.4/src/py_cq/metric_aggregator.py +14 -0
- python_code_quality-0.1.4/src/py_cq/parsers/__init__.py +0 -0
- python_code_quality-0.1.4/src/py_cq/parsers/banditparser.py +52 -0
- python_code_quality-0.1.4/src/py_cq/parsers/common.py +87 -0
- python_code_quality-0.1.4/src/py_cq/parsers/compileparser.py +134 -0
- python_code_quality-0.1.4/src/py_cq/parsers/complexityparser.py +86 -0
- python_code_quality-0.1.4/src/py_cq/parsers/coverageparser.py +88 -0
- python_code_quality-0.1.4/src/py_cq/parsers/halsteadparser.py +174 -0
- python_code_quality-0.1.4/src/py_cq/parsers/interrogateparser.py +58 -0
- python_code_quality-0.1.4/src/py_cq/parsers/maintainabilityparser.py +63 -0
- python_code_quality-0.1.4/src/py_cq/parsers/pytestparser.py +81 -0
- python_code_quality-0.1.4/src/py_cq/parsers/ruffparser.py +61 -0
- python_code_quality-0.1.4/src/py_cq/parsers/typarser.py +65 -0
- python_code_quality-0.1.4/src/py_cq/parsers/vultureparser.py +48 -0
- python_code_quality-0.1.4/src/py_cq/py.typed +0 -0
- python_code_quality-0.1.4/src/py_cq/storage.py +27 -0
- python_code_quality-0.1.4/src/py_cq/tool_registry.py +36 -0
- python_code_quality-0.1.4/tests/conftest.py +7 -0
- python_code_quality-0.1.4/tests/test_common.py +55 -0
- python_code_quality-0.1.4/tests/test_config.py +84 -0
- python_code_quality-0.1.4/tests/test_context_hash.py +81 -0
- python_code_quality-0.1.4/tests/test_execution_engine.py +127 -0
- python_code_quality-0.1.4/tests/test_llm_formatter.py +237 -0
- python_code_quality-0.1.4/tests/test_localtypes.py +59 -0
- python_code_quality-0.1.4/tests/test_parser_bandit.py +66 -0
- python_code_quality-0.1.4/tests/test_parser_compile.py +61 -0
- python_code_quality-0.1.4/tests/test_parser_complexity.py +76 -0
- python_code_quality-0.1.4/tests/test_parser_coverage.py +59 -0
- python_code_quality-0.1.4/tests/test_parser_interrogate.py +72 -0
- python_code_quality-0.1.4/tests/test_parser_maintainability.py +74 -0
- python_code_quality-0.1.4/tests/test_parser_pytest.py +35 -0
- python_code_quality-0.1.4/tests/test_parser_ruff.py +33 -0
- python_code_quality-0.1.4/tests/test_parser_ty.py +33 -0
- python_code_quality-0.1.4/tests/test_parser_vulture.py +51 -0
- python_code_quality-0.1.4/tests/test_storage.py +41 -0
- python_code_quality-0.1.4/uv.lock +519 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# This workflow will upload a Python Package to PyPI when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
|
3
|
+
|
|
4
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
5
|
+
# They are provided by a third-party and are governed by
|
|
6
|
+
# separate terms of service, privacy policy, and support
|
|
7
|
+
# documentation.
|
|
8
|
+
|
|
9
|
+
name: Upload Python Package
|
|
10
|
+
|
|
11
|
+
on:
|
|
12
|
+
release:
|
|
13
|
+
types: [published]
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
release-build:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- name: Install uv
|
|
26
|
+
uses: astral-sh/setup-uv@v5
|
|
27
|
+
|
|
28
|
+
- name: Set up Python
|
|
29
|
+
run: uv python install
|
|
30
|
+
|
|
31
|
+
- name: Build release distributions
|
|
32
|
+
run: |
|
|
33
|
+
uv build
|
|
34
|
+
|
|
35
|
+
- name: Upload distributions
|
|
36
|
+
uses: actions/upload-artifact@v4
|
|
37
|
+
with:
|
|
38
|
+
name: release-dists
|
|
39
|
+
path: dist/
|
|
40
|
+
|
|
41
|
+
pypi-publish:
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
needs:
|
|
44
|
+
- release-build
|
|
45
|
+
permissions:
|
|
46
|
+
# IMPORTANT: this permission is mandatory for trusted publishing
|
|
47
|
+
id-token: write
|
|
48
|
+
|
|
49
|
+
# Dedicated environments with protections for publishing are strongly recommended.
|
|
50
|
+
# For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
|
|
51
|
+
environment:
|
|
52
|
+
name: pypi
|
|
53
|
+
# OPTIONAL: uncomment and update to include your PyPI project URL in the deployment status:
|
|
54
|
+
# url: https://pypi.org/p/YOURPROJECT
|
|
55
|
+
#
|
|
56
|
+
# ALTERNATIVE: if your GitHub Release name is the PyPI project version string
|
|
57
|
+
# ALTERNATIVE: exactly, uncomment the following line instead:
|
|
58
|
+
url: https://pypi.org/project/python-code-quality/${{ github.event.release.name }}
|
|
59
|
+
|
|
60
|
+
steps:
|
|
61
|
+
- name: Retrieve release distributions
|
|
62
|
+
uses: actions/download-artifact@v4
|
|
63
|
+
with:
|
|
64
|
+
name: release-dists
|
|
65
|
+
path: dist/
|
|
66
|
+
|
|
67
|
+
- name: Publish release distributions to PyPI
|
|
68
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
69
|
+
with:
|
|
70
|
+
packages-dir: dist/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.14
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
CQ is a Python CLI tool for iterative, LLM-assisted code improvement. The primary use case is:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
cq check -o llm # returns the single most critical defect as markdown
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The LLM fixes it, the user re-runs, and repeats until all tools pass. CQ runs
|
|
14
|
+
11 static analysis tools in priority order (compile → security → lint → types →
|
|
15
|
+
tests → coverage → complexity → dead code → style) and aggregates results into
|
|
16
|
+
a single score.
|
|
17
|
+
|
|
18
|
+
## Commands
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# Run tests
|
|
22
|
+
uv run pytest
|
|
23
|
+
|
|
24
|
+
# Run a single test
|
|
25
|
+
uv run pytest tests/test_common.py::test_score_logistic_variant
|
|
26
|
+
|
|
27
|
+
# Run the CLI
|
|
28
|
+
uv run cq check # table output, current directory
|
|
29
|
+
uv run cq check -o llm # LLM markdown, primary use case
|
|
30
|
+
uv run cq check -o score # numeric score
|
|
31
|
+
uv run cq check path/to/project/
|
|
32
|
+
|
|
33
|
+
# Lint
|
|
34
|
+
uv run ruff check src/
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Architecture
|
|
38
|
+
|
|
39
|
+
**Pipeline flow:** CLI (`cli.py`) → tool registry → execution engine → parsers → metric aggregator → output
|
|
40
|
+
|
|
41
|
+
- **`cli.py`**: Typer app with a single `check` command. Output mode selected via `--output`/`-o` enum (`table`, `score`, `json`, `llm`). Runs tools in parallel by default. Reads `[tool.cq]` from the target project's `pyproject.toml` and applies overrides before running.
|
|
42
|
+
- **`tool_registry.py`**: Loads `src/cq/config/tools.yaml` at import time via `importlib.resources`, dynamically imports parser classes, builds a `dict[str, ToolConfig]` registry.
|
|
43
|
+
- **`config/tools.yaml`** (at `src/cq/config/tools.yaml`): Declares each analysis tool: shell command template (with `{context_path}` placeholder), parser class name, priority, warning/error thresholds. Tools are listed and executed in priority order.
|
|
44
|
+
- **`execution_engine.py`**: Runs shell commands via `subprocess.run`, caches results with `diskcache` using a content-based hash. Parallel execution via `ThreadPoolExecutor`; results are sorted by priority before returning.
|
|
45
|
+
- **`parsers/`**: Each parser subclasses `AbstractParser` (from `localtypes.py`), implementing `parse(RawResult) -> ToolResult` and optionally `format_llm_message(ToolResult) -> str`. Parser module names must match the lowercase parser class name (e.g., `PytestParser` → `pytestparser.py`).
|
|
46
|
+
- **`localtypes.py`**: Core dataclasses — `ToolConfig`, `RawResult`, `ToolResult`, `CombinedToolResults`, and `AbstractParser` ABC.
|
|
47
|
+
- **`metric_aggregator.py`**: Wraps results into `CombinedToolResults`, which computes an overall score as the average of per-tool mean metrics.
|
|
48
|
+
- **`llm_formatter.py`**: Selects the worst-scoring tool by severity tier then priority, formats its top defect as markdown for LLM consumption.
|
|
49
|
+
|
|
50
|
+
## Adding a New Analysis Tool
|
|
51
|
+
|
|
52
|
+
1. Add tool entry in `config/tools.yaml` with command template, parser name, priority, and thresholds.
|
|
53
|
+
2. Create `src/cq/parsers/<parsername>.py` with a class matching the `parser` field in YAML.
|
|
54
|
+
3. The parser must subclass `AbstractParser` and implement `parse(RawResult) -> ToolResult`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chris Kilner <chris@rhiza.fr>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-code-quality
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: Python Code Quality Analysis Tool - feed the results from 11 CQ CQ straight into an LLM.
|
|
5
|
+
Project-URL: Homepage, https://github.com/rhiza-fr/py-cq
|
|
6
|
+
Project-URL: Repository, https://github.com/rhiza-fr/py-cq
|
|
7
|
+
Author-email: Chris Kilner <chris@rhiza.fr>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Requires-Dist: bandit>=1.8.0
|
|
12
|
+
Requires-Dist: coverage>=7.8.2
|
|
13
|
+
Requires-Dist: diskcache>=5.6.3
|
|
14
|
+
Requires-Dist: interrogate>=1.7.0
|
|
15
|
+
Requires-Dist: pytest-cov>=6.1.1
|
|
16
|
+
Requires-Dist: pytest-json-report>=1.5.0
|
|
17
|
+
Requires-Dist: pytest>=8.4.0
|
|
18
|
+
Requires-Dist: pyyaml>=6.0.2
|
|
19
|
+
Requires-Dist: radon>=6.0.1
|
|
20
|
+
Requires-Dist: rich>=14.0.0
|
|
21
|
+
Requires-Dist: ruff>=0.14.1
|
|
22
|
+
Requires-Dist: ty>=0.0.17
|
|
23
|
+
Requires-Dist: typer>=0.16.0
|
|
24
|
+
Requires-Dist: vulture>=2.14
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# CQ - Python Code Quality Analysis Tool
|
|
28
|
+
|
|
29
|
+
Python Code Quality Analysis Tool - feed the results from 11 CQ tools straight into an LLM. The primary workflow is:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
cq check -o llm # get the single most critical defect as markdown
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Feed that output to an LLM, apply the fix, repeat until the score is clean.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
uv tool install python-code-quality
|
|
41
|
+
|
|
42
|
+
# or
|
|
43
|
+
git pull https://github.com/rhiza-fr/py-cq.git
|
|
44
|
+
cd py-cq
|
|
45
|
+
uv tool install .
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Tools
|
|
49
|
+
|
|
50
|
+
CQ runs these tools in *parallel*:
|
|
51
|
+
|
|
52
|
+
| Priority | Tool | Measures |
|
|
53
|
+
|----------|------|----------|
|
|
54
|
+
| 1 | compileall | Syntax errors |
|
|
55
|
+
| 2 | bandit | Security vulnerabilities |
|
|
56
|
+
| 3 | ruff | Lint / style |
|
|
57
|
+
| 4 | ty | Type errors |
|
|
58
|
+
| 5 | pytest | Test pass rate |
|
|
59
|
+
| 6 | coverage | Test coverage |
|
|
60
|
+
| 7 | radon cc | Cyclomatic complexity |
|
|
61
|
+
| 8 | radon mi | Maintainability index |
|
|
62
|
+
| 9 | radon hal | Halstead volume / bug estimate |
|
|
63
|
+
| 10 | vulture | Dead code |
|
|
64
|
+
| 11 | interrogate | Docstring coverage |
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# LLM workflow: get the top defect as markdown (primary use case)
|
|
70
|
+
cq check -o llm
|
|
71
|
+
|
|
72
|
+
# Rich table with all metrics (default, also saves .cq.json)
|
|
73
|
+
cq check
|
|
74
|
+
|
|
75
|
+
# Numeric score only — useful in CI or scripts
|
|
76
|
+
cq check -o score
|
|
77
|
+
|
|
78
|
+
# Full JSON output
|
|
79
|
+
cq check -o json
|
|
80
|
+
|
|
81
|
+
# Explicit path (defaults to current directory)
|
|
82
|
+
cq check path/to/project/
|
|
83
|
+
cq check path/to/file.py
|
|
84
|
+
|
|
85
|
+
# Run sequentially (1 worker) instead of in parallel
|
|
86
|
+
cq check --workers 1
|
|
87
|
+
|
|
88
|
+
# Clear cached results before running
|
|
89
|
+
cq check --clear-cache
|
|
90
|
+
|
|
91
|
+
# Save table output to a custom file
|
|
92
|
+
cq check --out-file custom_results.json
|
|
93
|
+
|
|
94
|
+
# Show effective tool configuration (thresholds, enabled/disabled status)
|
|
95
|
+
cq config
|
|
96
|
+
cq config path/to/project/
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Output
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
> cq check .
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┓
|
|
107
|
+
┃ Tool ┃ Time ┃ Metric ┃ Score ┃ Status ┃
|
|
108
|
+
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
|
|
109
|
+
│ compile │ 0.42s │ compile │ 1.000 │ OK │
|
|
110
|
+
│ bandit │ 0.56s │ security │ 1.000 │ OK │
|
|
111
|
+
│ ruff │ 0.17s │ lint │ 1.000 │ OK │
|
|
112
|
+
│ ty │ 0.33s │ type_check │ 1.000 │ OK │
|
|
113
|
+
│ pytest │ 0.91s │ tests │ 1.000 │ OK │
|
|
114
|
+
│ coverage │ 1.26s │ coverage │ 0.910 │ OK │
|
|
115
|
+
│ radon cc │ 0.32s │ simplicity │ 0.982 │ OK │
|
|
116
|
+
│ radon mi │ 0.38s │ maintainability │ 0.869 │ OK │
|
|
117
|
+
│ radon hal │ 0.30s │ file_bug_free │ 0.928 │ OK │
|
|
118
|
+
│ radon hal │ │ file_smallness │ 0.851 │ OK │
|
|
119
|
+
│ radon hal │ │ functions_bug_free │ 0.913 │ OK │
|
|
120
|
+
│ radon hal │ │ functions_smallness │ 0.724 │ OK │
|
|
121
|
+
│ vulture │ 0.32s │ dead_code │ 1.000 │ OK │
|
|
122
|
+
│ interrogate │ 0.36s │ doc_coverage │ 1.000 │ OK │
|
|
123
|
+
│ │ │ Score │ 0.965 │ │
|
|
124
|
+
└──────────────────┴──────────┴───────────────────────────┴─────────┴──────────┘
|
|
125
|
+
```
|
|
126
|
+
```bash
|
|
127
|
+
> cq check . -o score
|
|
128
|
+
```
|
|
129
|
+
```python
|
|
130
|
+
0.9662730667181059 # this is designed to approach but not reach 1.0
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
> cq check . -o llm
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```md
|
|
138
|
+
`data/problems/travelling_salesman/ts_bad.py:21` — **F841**: Local variable `unused_variable` is assigned to but never used
|
|
139
|
+
|
|
140
|
+
18: min_dist = float("inf")
|
|
141
|
+
19: nearest_city = None
|
|
142
|
+
20: for city in cities:
|
|
143
|
+
21: unused_variable = 67
|
|
144
|
+
22: dist = calc_dist(current_city, city)
|
|
145
|
+
23: if dist < min_dist:
|
|
146
|
+
24: min_dist = dist
|
|
147
|
+
25: nearest_city = city
|
|
148
|
+
|
|
149
|
+
Please fix only this issue. After fixing, run `cq check . -o llm` to verify.
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Configuration
|
|
153
|
+
|
|
154
|
+
Add a `[tool.cq]` section to your project's `pyproject.toml`:
|
|
155
|
+
|
|
156
|
+
```toml
|
|
157
|
+
[tool.cq]
|
|
158
|
+
# Skip tools that are slow or not relevant to your project
|
|
159
|
+
disable = ["coverage", "interrogate"]
|
|
160
|
+
|
|
161
|
+
# Override warning/error thresholds per tool
|
|
162
|
+
[tool.cq.thresholds.coverage]
|
|
163
|
+
warning = 0.9
|
|
164
|
+
error = 0.7
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Tool IDs match the keys in `config/tools.yaml`: `compilation`, `bandit`, `ruff`, `ty`, `pytest`, `coverage`, `complexity`, `maintainability`, `halstead`, `vulture`, `interrogate`.
|
|
168
|
+
|
|
169
|
+
## LLM workflow
|
|
170
|
+
|
|
171
|
+
`-o llm` selects the single worst-scoring tool and formats its top defect as
|
|
172
|
+
concise markdown. The LLM fixes it, you re-run `cq check -o llm`, and repeat
|
|
173
|
+
until all tools are green. Priority order ensures the most critical category
|
|
174
|
+
(security, type errors, failing tests) is fixed before cosmetic ones.
|
|
175
|
+
|
|
176
|
+
## Tools
|
|
177
|
+
|
|
178
|
+
Many thanks to all the wonderful maintainers of :
|
|
179
|
+
|
|
180
|
+
- [compileall](https://docs.python.org/3/library/compileall.html)
|
|
181
|
+
- [bandit](https://github.com/PyCQA/bandit)
|
|
182
|
+
- [ruff](https://github.com/astral-sh/ruff)
|
|
183
|
+
- [ty](https://github.com/astral-sh/ty)
|
|
184
|
+
- [pytest](https://github.com/pytest-dev/pytest)
|
|
185
|
+
- [coverage.py](https://github.com/nedbat/coveragepy)
|
|
186
|
+
- [radon](https://github.com/rubik/radon)
|
|
187
|
+
- [vulture](https://github.com/jendrikseipp/vulture)
|
|
188
|
+
- [interrogate](https://github.com/econchick/interrogate)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# CQ - Python Code Quality Analysis Tool
|
|
2
|
+
|
|
3
|
+
Python Code Quality Analysis Tool - feed the results from 11 CQ tools straight into an LLM. The primary workflow is:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
cq check -o llm # get the single most critical defect as markdown
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Feed that output to an LLM, apply the fix, repeat until the score is clean.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv tool install python-code-quality
|
|
15
|
+
|
|
16
|
+
# or
|
|
17
|
+
git pull https://github.com/rhiza-fr/py-cq.git
|
|
18
|
+
cd py-cq
|
|
19
|
+
uv tool install .
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Tools
|
|
23
|
+
|
|
24
|
+
CQ runs these tools in *parallel*:
|
|
25
|
+
|
|
26
|
+
| Priority | Tool | Measures |
|
|
27
|
+
|----------|------|----------|
|
|
28
|
+
| 1 | compileall | Syntax errors |
|
|
29
|
+
| 2 | bandit | Security vulnerabilities |
|
|
30
|
+
| 3 | ruff | Lint / style |
|
|
31
|
+
| 4 | ty | Type errors |
|
|
32
|
+
| 5 | pytest | Test pass rate |
|
|
33
|
+
| 6 | coverage | Test coverage |
|
|
34
|
+
| 7 | radon cc | Cyclomatic complexity |
|
|
35
|
+
| 8 | radon mi | Maintainability index |
|
|
36
|
+
| 9 | radon hal | Halstead volume / bug estimate |
|
|
37
|
+
| 10 | vulture | Dead code |
|
|
38
|
+
| 11 | interrogate | Docstring coverage |
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# LLM workflow: get the top defect as markdown (primary use case)
|
|
44
|
+
cq check -o llm
|
|
45
|
+
|
|
46
|
+
# Rich table with all metrics (default, also saves .cq.json)
|
|
47
|
+
cq check
|
|
48
|
+
|
|
49
|
+
# Numeric score only — useful in CI or scripts
|
|
50
|
+
cq check -o score
|
|
51
|
+
|
|
52
|
+
# Full JSON output
|
|
53
|
+
cq check -o json
|
|
54
|
+
|
|
55
|
+
# Explicit path (defaults to current directory)
|
|
56
|
+
cq check path/to/project/
|
|
57
|
+
cq check path/to/file.py
|
|
58
|
+
|
|
59
|
+
# Run sequentially (1 worker) instead of in parallel
|
|
60
|
+
cq check --workers 1
|
|
61
|
+
|
|
62
|
+
# Clear cached results before running
|
|
63
|
+
cq check --clear-cache
|
|
64
|
+
|
|
65
|
+
# Save table output to a custom file
|
|
66
|
+
cq check --out-file custom_results.json
|
|
67
|
+
|
|
68
|
+
# Show effective tool configuration (thresholds, enabled/disabled status)
|
|
69
|
+
cq config
|
|
70
|
+
cq config path/to/project/
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Output
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
> cq check .
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┓
|
|
81
|
+
┃ Tool ┃ Time ┃ Metric ┃ Score ┃ Status ┃
|
|
82
|
+
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
|
|
83
|
+
│ compile │ 0.42s │ compile │ 1.000 │ OK │
|
|
84
|
+
│ bandit │ 0.56s │ security │ 1.000 │ OK │
|
|
85
|
+
│ ruff │ 0.17s │ lint │ 1.000 │ OK │
|
|
86
|
+
│ ty │ 0.33s │ type_check │ 1.000 │ OK │
|
|
87
|
+
│ pytest │ 0.91s │ tests │ 1.000 │ OK │
|
|
88
|
+
│ coverage │ 1.26s │ coverage │ 0.910 │ OK │
|
|
89
|
+
│ radon cc │ 0.32s │ simplicity │ 0.982 │ OK │
|
|
90
|
+
│ radon mi │ 0.38s │ maintainability │ 0.869 │ OK │
|
|
91
|
+
│ radon hal │ 0.30s │ file_bug_free │ 0.928 │ OK │
|
|
92
|
+
│ radon hal │ │ file_smallness │ 0.851 │ OK │
|
|
93
|
+
│ radon hal │ │ functions_bug_free │ 0.913 │ OK │
|
|
94
|
+
│ radon hal │ │ functions_smallness │ 0.724 │ OK │
|
|
95
|
+
│ vulture │ 0.32s │ dead_code │ 1.000 │ OK │
|
|
96
|
+
│ interrogate │ 0.36s │ doc_coverage │ 1.000 │ OK │
|
|
97
|
+
│ │ │ Score │ 0.965 │ │
|
|
98
|
+
└──────────────────┴──────────┴───────────────────────────┴─────────┴──────────┘
|
|
99
|
+
```
|
|
100
|
+
```bash
|
|
101
|
+
> cq check . -o score
|
|
102
|
+
```
|
|
103
|
+
```python
|
|
104
|
+
0.9662730667181059 # this is designed to approach but not reach 1.0
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
> cq check . -o llm
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```md
|
|
112
|
+
`data/problems/travelling_salesman/ts_bad.py:21` — **F841**: Local variable `unused_variable` is assigned to but never used
|
|
113
|
+
|
|
114
|
+
18: min_dist = float("inf")
|
|
115
|
+
19: nearest_city = None
|
|
116
|
+
20: for city in cities:
|
|
117
|
+
21: unused_variable = 67
|
|
118
|
+
22: dist = calc_dist(current_city, city)
|
|
119
|
+
23: if dist < min_dist:
|
|
120
|
+
24: min_dist = dist
|
|
121
|
+
25: nearest_city = city
|
|
122
|
+
|
|
123
|
+
Please fix only this issue. After fixing, run `cq check . -o llm` to verify.
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Configuration
|
|
127
|
+
|
|
128
|
+
Add a `[tool.cq]` section to your project's `pyproject.toml`:
|
|
129
|
+
|
|
130
|
+
```toml
|
|
131
|
+
[tool.cq]
|
|
132
|
+
# Skip tools that are slow or not relevant to your project
|
|
133
|
+
disable = ["coverage", "interrogate"]
|
|
134
|
+
|
|
135
|
+
# Override warning/error thresholds per tool
|
|
136
|
+
[tool.cq.thresholds.coverage]
|
|
137
|
+
warning = 0.9
|
|
138
|
+
error = 0.7
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Tool IDs match the keys in `config/tools.yaml`: `compilation`, `bandit`, `ruff`, `ty`, `pytest`, `coverage`, `complexity`, `maintainability`, `halstead`, `vulture`, `interrogate`.
|
|
142
|
+
|
|
143
|
+
## LLM workflow
|
|
144
|
+
|
|
145
|
+
`-o llm` selects the single worst-scoring tool and formats its top defect as
|
|
146
|
+
concise markdown. The LLM fixes it, you re-run `cq check -o llm`, and repeat
|
|
147
|
+
until all tools are green. Priority order ensures the most critical category
|
|
148
|
+
(security, type errors, failing tests) is fixed before cosmetic ones.
|
|
149
|
+
|
|
150
|
+
## Tools
|
|
151
|
+
|
|
152
|
+
Many thanks to all the wonderful maintainers of :
|
|
153
|
+
|
|
154
|
+
- [compileall](https://docs.python.org/3/library/compileall.html)
|
|
155
|
+
- [bandit](https://github.com/PyCQA/bandit)
|
|
156
|
+
- [ruff](https://github.com/astral-sh/ruff)
|
|
157
|
+
- [ty](https://github.com/astral-sh/ty)
|
|
158
|
+
- [pytest](https://github.com/pytest-dev/pytest)
|
|
159
|
+
- [coverage.py](https://github.com/nedbat/coveragepy)
|
|
160
|
+
- [radon](https://github.com/rubik/radon)
|
|
161
|
+
- [vulture](https://github.com/jendrikseipp/vulture)
|
|
162
|
+
- [interrogate](https://github.com/econchick/interrogate)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Intentionally bad implementation of the Travelling Salesman Problem for testing purposes."""
|
|
2
|
+
import math
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
# Bad Variable Naming
|
|
6
|
+
# cities = [(0, 0), (1, 5), (5, 3), (3, 1), (1, 4)]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Bad Method Naming
|
|
10
|
+
def calc_dist(city1, city2):
|
|
11
|
+
"""Calculates the distance between two cities."""
|
|
12
|
+
return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Missing Docstrings
|
|
16
|
+
def find_nearest_city(cities, current_city):
|
|
17
|
+
"""Finds the nearest city to the current city."""
|
|
18
|
+
min_dist = float("inf")
|
|
19
|
+
nearest_city = None
|
|
20
|
+
for city in cities:
|
|
21
|
+
# unused = 'bug'
|
|
22
|
+
dist = calc_dist(current_city, city)
|
|
23
|
+
if dist < min_dist:
|
|
24
|
+
min_dist = dist
|
|
25
|
+
nearest_city = city
|
|
26
|
+
return nearest_city
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Inefficient Loop
|
|
30
|
+
def generate_tour(cities, start_city):
|
|
31
|
+
"""Generates a tour starting from start_city visiting all cities."""
|
|
32
|
+
#error = {a = b}
|
|
33
|
+
tour = [start_city]
|
|
34
|
+
total_distance = 0
|
|
35
|
+
unvisited = set(cities)
|
|
36
|
+
unvisited.remove(start_city)
|
|
37
|
+
|
|
38
|
+
time.sleep(0.1) # Simulating a delay for no reason
|
|
39
|
+
while unvisited:
|
|
40
|
+
nearest = find_nearest_city(cities, tour[-1])
|
|
41
|
+
tour.append(nearest)
|
|
42
|
+
total_distance += calc_dist(tour[-2], tour[-1])
|
|
43
|
+
unvisited.remove(nearest)
|
|
44
|
+
|
|
45
|
+
tour.append(tour[0]) # Return to start
|
|
46
|
+
total_distance += calc_dist(tour[-1], tour[0])
|
|
47
|
+
return tour, total_distance
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Poor Memory Use (Not a major issue here, but illustrates the point)
|
|
51
|
+
# Could be improved by using generators or iterators if dealing with very large datasets.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def main():
|
|
55
|
+
"""Runs the bad TSP implementation on a sample set of cities."""
|
|
56
|
+
cities = [(0, 0), (1, 5), (5, 3), (3, 1), (1, 4)]
|
|
57
|
+
|
|
58
|
+
tour, total_distance = generate_tour(cities, cities[0])
|
|
59
|
+
|
|
60
|
+
print("Tour:", tour)
|
|
61
|
+
print("Total Distance:", total_distance)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
if __name__ == "__main__":
|
|
65
|
+
main()
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Good implementation of the Travelling Salesman Problem using nearest-neighbor heuristic."""
|
|
2
|
+
import math
|
|
3
|
+
import random
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def distance(city1, city2):
|
|
7
|
+
"""Calculates the Euclidean distance between two cities."""
|
|
8
|
+
return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_calc_dist():
|
|
12
|
+
"""Tests the calc_dist function."""
|
|
13
|
+
assert distance((0, 0), (3, 4)) == 5.0, "Test failed!"
|
|
14
|
+
assert distance((1, 1), (4, 5)) == 5.0, "Test failed!"
|
|
15
|
+
# assert False
|
|
16
|
+
print("All tests passed!")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def nearest_neighbor(cities):
|
|
20
|
+
"""Implements the Nearest Neighbor heuristic for the TSP."""
|
|
21
|
+
current_city = random.choice(cities) # Start at a random city
|
|
22
|
+
unvisited_cities = set(cities)
|
|
23
|
+
unvisited_cities.remove(current_city)
|
|
24
|
+
tour = [current_city]
|
|
25
|
+
total_distance = 0
|
|
26
|
+
|
|
27
|
+
while unvisited_cities:
|
|
28
|
+
nearest_city = None
|
|
29
|
+
min_dist = float("inf")
|
|
30
|
+
|
|
31
|
+
for city in unvisited_cities:
|
|
32
|
+
dist = distance(current_city, city)
|
|
33
|
+
if dist < min_dist:
|
|
34
|
+
min_dist = dist
|
|
35
|
+
nearest_city = city
|
|
36
|
+
|
|
37
|
+
tour.append(nearest_city)
|
|
38
|
+
total_distance += min_dist
|
|
39
|
+
current_city = nearest_city
|
|
40
|
+
unvisited_cities.remove(current_city)
|
|
41
|
+
|
|
42
|
+
# Return to the starting city
|
|
43
|
+
total_distance += distance(current_city, cities[0])
|
|
44
|
+
tour.append(cities[0])
|
|
45
|
+
|
|
46
|
+
return tour, total_distance
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def main():
|
|
50
|
+
"""Runs the nearest-neighbor TSP on a sample set of cities."""
|
|
51
|
+
# Example cities (you can change these)
|
|
52
|
+
cities = [(0, 0), (1, 5), (5, 3), (3, 1), (1, 4), (6, 12), (4, 1), (9, 13), (12, 4), (6, 14)]
|
|
53
|
+
|
|
54
|
+
tour, total_distance = nearest_neighbor(cities)
|
|
55
|
+
|
|
56
|
+
print("Tour:", tour)
|
|
57
|
+
print("Total Distance:", total_distance)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
if __name__ == "__main__":
|
|
61
|
+
main()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "python-code-quality"
|
|
3
|
+
version = "0.1.4"
|
|
4
|
+
description = "Python Code Quality Analysis Tool - feed the results from 11 CQ CQ straight into an LLM."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
authors = [{name = "Chris Kilner", email = "chris@rhiza.fr"}]
|
|
9
|
+
|
|
10
|
+
dependencies = [
|
|
11
|
+
"bandit>=1.8.0",
|
|
12
|
+
"coverage>=7.8.2",
|
|
13
|
+
"diskcache>=5.6.3",
|
|
14
|
+
"interrogate>=1.7.0",
|
|
15
|
+
"pytest>=8.4.0",
|
|
16
|
+
"pytest-cov>=6.1.1",
|
|
17
|
+
"pytest-json-report>=1.5.0",
|
|
18
|
+
"pyyaml>=6.0.2",
|
|
19
|
+
"radon>=6.0.1",
|
|
20
|
+
"rich>=14.0.0",
|
|
21
|
+
"ruff>=0.14.1",
|
|
22
|
+
"ty>=0.0.17",
|
|
23
|
+
"typer>=0.16.0",
|
|
24
|
+
"vulture>=2.14",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/rhiza-fr/py-cq"
|
|
29
|
+
Repository = "https://github.com/rhiza-fr/py-cq"
|
|
30
|
+
|
|
31
|
+
[build-system]
|
|
32
|
+
requires = ["hatchling"]
|
|
33
|
+
build-backend = "hatchling.build"
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.wheel]
|
|
36
|
+
packages = ["src/py_cq"]
|
|
37
|
+
|
|
38
|
+
[project.scripts]
|
|
39
|
+
cq = "py_cq.main:main"
|