|
1 | 1 | # python-doctor |
2 | 2 |
|
3 | | -Diagnose your Python project's health. Get a score, find issues, fix them. |
| 3 | +[](https://pypi.org/project/python-doctor/) |
| 4 | +[](https://pypi.org/project/python-doctor/) |
| 5 | + |
| 6 | +Diagnose your Python project's health. One command scans your codebase for security, performance, correctness, and architecture issues, then outputs a **0-100 score** with actionable diagnostics. |
| 7 | + |
| 8 | +Inspired by [react-doctor](https://github.com/millionco/react-doctor). |
| 9 | + |
| 10 | +## How it works |
| 11 | + |
| 12 | +Python Doctor detects your framework (Django, FastAPI, Flask), Python version, package manager (uv, poetry, pip), and test framework, then runs two analysis passes **in parallel**: |
| 13 | + |
| 14 | +1. **Lint**: Checks 30+ rules across security, performance, architecture, correctness, and framework-specific categories. Rules are toggled automatically based on your project setup. |
| 15 | +2. **Dead code**: Detects unused functions, classes, imports, and variables via [Vulture](https://github.com/jendrikseipp/vulture). |
| 16 | + |
| 17 | +Diagnostics are filtered through your config, then scored by severity (errors weigh more than warnings) to produce a **0-100 health score** (75+ Great, 50-74 Needs Work, <50 Critical). |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +Run instantly with uvx (no install needed): |
| 22 | + |
| 23 | +```bash |
| 24 | +uvx python-doctor . |
| 25 | +``` |
| 26 | + |
| 27 | +Or install globally: |
| 28 | + |
| 29 | +```bash |
| 30 | +uv tool install python-doctor |
| 31 | +# or |
| 32 | +pip install python-doctor |
| 33 | +``` |
| 34 | + |
| 35 | +Use `--verbose` to see affected files and line numbers: |
| 36 | + |
| 37 | +```bash |
| 38 | +python-doctor . --verbose |
| 39 | +``` |
| 40 | + |
| 41 | +## Install for your coding agent |
| 42 | + |
| 43 | +Add the skill to your Claude Code, Cursor, or other AI coding agent: |
| 44 | + |
| 45 | +```bash |
| 46 | +# Claude Code |
| 47 | +cp skills/python-doctor/SKILL.md .claude/skills/python-doctor.md |
| 48 | +``` |
| 49 | + |
| 50 | +Or reference the AGENTS.md in your project root — it's automatically picked up by Claude Code, Cursor, Windsurf, and others. |
| 51 | + |
| 52 | +## GitHub Actions |
| 53 | + |
| 54 | +```yaml |
| 55 | +- uses: actions/checkout@v5 |
| 56 | + with: |
| 57 | + fetch-depth: 0 # required for --diff |
| 58 | +- uses: actions/setup-python@v5 |
| 59 | + with: |
| 60 | + python-version: "3.12" |
| 61 | +- name: Run Python Doctor |
| 62 | + run: | |
| 63 | + pip install python-doctor |
| 64 | + python-doctor . --verbose --diff main --fail-on error |
| 65 | +``` |
| 66 | +
|
| 67 | +## Options |
| 68 | +
|
| 69 | +``` |
| 70 | +Usage: python-doctor [OPTIONS] [DIRECTORY] |
| 71 | + |
| 72 | +Options: |
| 73 | + -v, --version Show the version and exit. |
| 74 | + --lint / --no-lint Enable/disable lint checks. |
| 75 | + --dead-code / --no-dead-code Enable/disable dead code detection. |
| 76 | + --verbose Show file details per rule. |
| 77 | + --score Output only the numeric score. |
| 78 | + --diff TEXT Scan only files changed vs base branch. |
| 79 | + --fail-on [error|warning|none] Exit with code 1 on this severity level. |
| 80 | + -h, --help Show this message and exit. |
| 81 | +``` |
| 82 | + |
| 83 | +## Configuration |
| 84 | + |
| 85 | +Create a `python-doctor.toml` in your project root: |
| 86 | + |
| 87 | +```toml |
| 88 | +[options] |
| 89 | +lint = true |
| 90 | +dead_code = true |
| 91 | +verbose = false |
| 92 | +fail_on = "none" |
| 93 | + |
| 94 | +[ignore] |
| 95 | +rules = ["no-import-in-function", "dead-code"] |
| 96 | +files = ["tests/fixtures/**", "migrations/**"] |
| 97 | +``` |
| 98 | + |
| 99 | +Or use `pyproject.toml`: |
| 100 | + |
| 101 | +```toml |
| 102 | +[tool.python-doctor] |
| 103 | +lint = true |
| 104 | +dead_code = true |
| 105 | + |
| 106 | +[tool.python-doctor.ignore] |
| 107 | +rules = ["no-import-in-function"] |
| 108 | +files = ["tests/fixtures/**"] |
| 109 | +``` |
| 110 | + |
| 111 | +If both exist, `python-doctor.toml` takes precedence. CLI flags always override config values. |
| 112 | + |
| 113 | +## Rules |
| 114 | + |
| 115 | +### Security |
| 116 | +| Rule | Severity | Description | |
| 117 | +|------|----------|-------------| |
| 118 | +| `no-eval` | Error | `eval()` executes arbitrary code | |
| 119 | +| `no-exec` | Error | `exec()` executes arbitrary code | |
| 120 | +| `no-pickle-load` | Error | `pickle.load()` can execute arbitrary code | |
| 121 | +| `no-unsafe-yaml-load` | Error | `yaml.load()` without Loader is unsafe | |
| 122 | +| `no-hardcoded-secret` | Error | Hardcoded secrets in source code | |
| 123 | +| `no-weak-hash` | Warning | MD5/SHA1 are cryptographically weak | |
| 124 | + |
| 125 | +### Performance |
| 126 | +| Rule | Severity | Description | |
| 127 | +|------|----------|-------------| |
| 128 | +| `no-string-concat-in-loop` | Warning | O(n^2) string concatenation | |
| 129 | +| `no-import-in-function` | Warning | Import re-executed on every call | |
| 130 | +| `no-star-import` | Warning | Pollutes namespace | |
| 131 | + |
| 132 | +### Architecture |
| 133 | +| Rule | Severity | Description | |
| 134 | +|------|----------|-------------| |
| 135 | +| `no-giant-module` | Warning | Module exceeds 500 lines | |
| 136 | +| `no-deep-nesting` | Warning | Nesting depth exceeds 5 | |
| 137 | +| `no-god-function` | Warning | Function exceeds 50 lines | |
| 138 | +| `no-too-many-args` | Warning | Function has more than 7 arguments | |
| 139 | + |
| 140 | +### Correctness |
| 141 | +| Rule | Severity | Description | |
| 142 | +|------|----------|-------------| |
| 143 | +| `no-mutable-default` | Error | Mutable default argument shared across calls | |
| 144 | +| `no-bare-except` | Warning | Catches SystemExit and KeyboardInterrupt | |
| 145 | +| `no-broad-except` | Warning | Catches overly broad Exception | |
| 146 | +| `no-assert-in-production` | Warning | Assert statements stripped with -O | |
| 147 | +| `no-return-in-init` | Warning | Return value in `__init__` | |
| 148 | + |
| 149 | +### Django |
| 150 | +| Rule | Severity | Description | |
| 151 | +|------|----------|-------------| |
| 152 | +| `no-raw-sql-injection` | Error | SQL built with string concatenation | |
| 153 | +| `no-debug-true` | Error | DEBUG = True hardcoded in settings | |
| 154 | +| `no-secret-key-in-source` | Error | SECRET_KEY hardcoded | |
| 155 | +| `no-n-plus-one-query` | Warning | Related object access in loop | |
| 156 | + |
| 157 | +### FastAPI |
| 158 | +| Rule | Severity | Description | |
| 159 | +|------|----------|-------------| |
| 160 | +| `no-sync-endpoint` | Warning | Sync def blocks the event loop | |
| 161 | +| `no-missing-response-model` | Warning | Endpoint missing response_model | |
| 162 | + |
| 163 | +### Flask |
| 164 | +| Rule | Severity | Description | |
| 165 | +|------|----------|-------------| |
| 166 | +| `no-flask-secret-in-source` | Error | Secret key hardcoded | |
| 167 | +| `no-flask-debug-mode` | Error | Debug mode in production | |
| 168 | +| `no-sql-string-format` | Error | SQL built with f-strings | |
| 169 | + |
| 170 | +### Dead Code |
| 171 | +| Rule | Severity | Description | |
| 172 | +|------|----------|-------------| |
| 173 | +| `dead-code` | Warning | Unused function, class, import, or variable | |
| 174 | + |
| 175 | +## Python API |
| 176 | + |
| 177 | +```python |
| 178 | +from python_doctor.api import diagnose |
| 179 | + |
| 180 | +result = diagnose("./path/to/your/project") |
| 181 | + |
| 182 | +print(result.score) # Score(value=82, label="Great") |
| 183 | +print(result.diagnostics) # List of Diagnostic objects |
| 184 | +print(result.project) # Detected framework, Python version, etc. |
| 185 | +``` |
| 186 | + |
| 187 | +## Contributing |
| 188 | + |
| 189 | +```bash |
| 190 | +git clone https://github.com/themohitkhare/pythondoctor |
| 191 | +cd pythondoctor |
| 192 | +uv sync --all-extras |
| 193 | +uv run pytest |
| 194 | +uv run python-doctor . # dogfood it |
| 195 | +``` |
| 196 | + |
| 197 | +## License |
| 198 | + |
| 199 | +MIT |
0 commit comments