AgentLint 实测验证 100/100基于 929 个 Python 开源仓库统计
Python CLAUDE.md & AGENTS.md 模板
pytest 分层单测、ruff 校验、uv/venv 虚拟环境隔离与严格类型注解,提炼自 929 个 Python 仓库。
# Python Project — Python Harness Python 3.12 service managed with `uv`, tested with `pytest`, formatted with `ruff`, and typechecked with `pyright`. ## Commands - Sync deps: `uv sync --dev` - Fast unit test: `uv run pytest tests/unit/test_module.py -x -q -m "not slow and not network"` - Full suite: `uv run pytest -q` - Lint & format: `uv run ruff check . && uv run ruff format --check .` - Typecheck: `uv run pyright` ## Rules - Don't invoke global `python` or `pip`. Instead prefix commands with `uv run` (or `.venv/bin/python`). Because system Python mismatches project dependencies. - Don't swallow exceptions with bare `except Exception: pass`. Instead catch specific errors and log structured context. Because silent fallbacks mask production data corruption. - Don't add tests that hit live network endpoints without `@pytest.mark.network`. Instead mock HTTP boundaries in `tests/unit/`. Because unmocked calls make CI flaky. - When modifying database models, read `docs/migrations.md` before generating Alembic scripts. ## Continuity & Safety - Keep `.env`, `.venv/`, and `CLAUDE.local.md` out of git. - Record architecture decisions and passing test evidence in `HANDOFF.md`.
Python 开源生态中最常采用的工程规则
基于 4,533 仓库研究中 929 个公开 Python 仓库的实测统计:
pytest测试
Use pytest as the testing framework
45.9%426 个仓库
unit-tests测试
Write unit tests for all changes
40.9%380 个仓库
error-handling代码规范
Proper error handling with context
30.4%282 个仓库
type-hints代码规范
Type hints on all function signatures
29.8%277 个仓库
ruff工具链
Use ruff for linting and formatting
29.1%270 个仓库
docker工具链
Docker for containerization
28.6%266 个仓库
conventional-commits工作流
Use conventional commit messages
27.2%253 个仓库
uv工具链
Use uv for package management
24.5%228 个仓库
必写验证命令(对应规则 W1 与 W7)
- 安装依赖
- uv sync --dev
- 快速单测(秒级迭代)
- uv run pytest tests/unit/test_target.py -x -q
- 全量测试
- uv run pytest -q
- Lint 与静态检查
- uv run ruff check . && uv run ruff format --check . && uv run pyright
- 生产构建
- uv build
Python 项目中 AI 代理常见踩坑
- 01AI 直接调用系统
python而非uv run/.venv/bin/python,引发大量误报缺包错误。 - 02宽泛的
except Exception: pass会在代理自我修复循环里吞掉真实异常。 - 03慢测试与联网测试未加 pytest marker,导致代理每次改代码都卡住数分钟。