File contents py-improve — Python 代码改进
self-contained skill。复制整个目录即可分享给其他项目。
🛑 MANDATORY WORKFLOW — check all before declaring done
Phase 0: Reconnaissance
Phase 1: Diagnose
Phase 2: Execute
Phase 3: Verify
包含
路径
内容
references/code-refactor.md
god-fn 拆分 + 死代码 7 步 + TDD 4 模式 + YAGNI
references/code-review-checklist.md
合入前机械规则 + no-silent-swallow P0
references/logging-observability.md
静默吞错扫描命令 + 修法模板
references/dict-dedup.md
字典/常量去重 4 类问题分类 + Phase 0-5
references/dict-dedup-case-levelcfg.md
_LEVEL_CFG CONFLICT 案例教学
scripts/silent_swallow.py
静默吞错扫描器
scripts/reorg_drift.py
引用残留扫描(删 module 后检查 caller)
mcp_servers/python_refactor_server.py
MCP server: ruff/vulture/bandit/radon/pyright
使用
# 静默吞错扫描
python ~/.claude/skills/py-improve/scripts/silent_swallow.py src/
# 引用残留
python ~/.claude/skills/py-improve/scripts/reorg_drift.py
# MCP 工具(注册到 ~/.claude/settings.json)
# 见 mcp_servers/python_refactor_server.py
14 硬约束(跨子工作流通用)
零新增依赖 + 零提前防御 (YAGNI) : 只用 stdlib + 已装库。不为假设风险提前加 try/except / retry / fallback / 抽象层。
commit 颗粒度 : 1 逻辑单元 = 1 commit,独立可回滚。
默认回滚 = rsync 备份还原 (无损)。绝对禁止 git reset --hard 。
死代码证明需 7 步 checklist : 静态引用 + 文本搜索 + 框架注册 + export + 动态调用 + 测试/生成 + 用户签字。0 caller grep ≠ 证明。
TDD 按场景分 4 模式 : Characterization / Red-Green / Structural (7 步 + 全 build/test) / Regression。
commit 前全量测试全绿 ,不破 CI。项目级例外 :当本项目跑测试本身不安全时(如测试会改动生产数据),以项目自己的规则为准——在运行报告里记录该例外,并跑能覆盖本次改动的最安全子集。
prod 锁定 : 不动线上代码,owner 显式授权才动。
宁缺勿伪 : 不确定的事实留 TODO,不编。校验靠 grep / codegraph / pyright 实测。
DB 删除必走退场流水线 : DROP 前 RENAME → PLAN_DELETE_<原名> → 7 天测试 → user 审。
批量任务先测最小 : 任何批量操作先选最小样本 (1-10) 跑通 + 计时,按比例推全量耗时。禁止直接开最大集合 。
daemon / 服务代码改动 4 步独立 : ①本地 Edit ②本地 py_compile 验证 ③scp 上传 + 清 pycache ④systemctl restart + pgrep 验证 PID 变了。不链式 restart && smoke 。
buffer 所有权被转移 : 缓存方必须保留副本,每次传递前 bytes.slice(0) / np.array(..., copy=True)。
hash 化构建产物必须整目录同步 : 前端 chunk 名带 hash — 只推改的文件 → index.html 引用新 hash → 缺 chunk → MIME text/html 404。
部署/发布后必须验证实际生效产物标识 : 脚本输出 "✓ done" ≠ 部署成功。
关联
/repo-medic — meta 入口
/doc-reorg — 重构后目录归档
/db-tweak — DB 相关调用方审查
仓库
github.com/liyong-labs/repo-medic — Apache-2.0。
1 --- 2 name: py-improve-2 3 description: py-improve — Python 代码改进 4 --- 5 6 # py-improve — Python 代码改进 7 8 self-contained skill。复制整个目录即可分享给其他项目。 9 10 ## 🛑 MANDATORY WORKFLOW — check all before declaring done 11 12 ### Phase 0: Reconnaissance 13 14 - [ ] **Read** `references/code-refactor.md` in full (17 phases methodology) 15 - [ ] **Read** `references/code-review-checklist.md` (合入前机械规则) 16 - [ ] **Read** `references/logging-observability.md` (静默吞错扫描命令) 17 - [ ] **Read** `references/dict-dedup.md` if dict/constant changes involved 18 - [ ] **Run** 静默吞错扫描: `python scripts/silent_swallow.py src/` 19 - [ ] **Run** 引用残留扫描: `python scripts/reorg_drift.py` (if renaming/moving) 20 - [ ] **Build** codegraph (大项目 ≥ 5min 时): `codegraph build --no-incremental` 21 - [ ] 🛑 **GATE**: 拿到 baseline metrics 后才能进 Phase 1(无基线 = 无改进证据) 22 23 ### Phase 1: Diagnose 24 25 - [ ] **List god-fn candidates**: cyclomatic > 15 OR 行数 > 100(`radon cc -s src/`) 26 - [ ] **List 死代码 candidates**: ruff `F401` / `F841` / pyright `reportUnused*` 27 - [ ] **List 重复**: 相同函数签名 ≥ 2 处(grep function def) 28 - [ ] **List 字典/常量重复**: `references/dict-dedup.md` 4 类问题分类对照 29 - [ ] 🛑 **GATE**: 列完候选 + 用户确认范围后才能动(避免无目标大改) 30 31 ### Phase 2: Execute 32 33 - [ ] **1 commit = 1 logical unit**(铁律 2)— 一次只改一类问题 34 - [ ] **写测试 first**(TDD 4 模式之一: Characterization / Red-Green / Regression / Structural) 35 - [ ] **改完立即跑** `pytest` + `ruff check` + `mypy`(铁律 6) 36 - [ ] **每个 commit 跑** `silent_swallow.py src/`(铁律横切 13.1) 37 - [ ] 🛑 **GATE**: 测试必须全绿才能下个 commit(红 = 退回 Phase 2) 38 39 ### Phase 3: Verify 40 41 - [ ] **所有测试绿**: `pytest tests/` 0 fail 42 - [ ] **无新增 silent-swallow**: `silent_swallow.py src/` output 不增 43 - [ ] **codegraph 无 broken import**: `codegraph where <moved_module>` 仍可解析 44 - [ ] **新增功能有 test**: `pytest --cov` 新代码有覆盖 45 - [ ] **commit 历史清晰**: `git log --oneline` 一目了然每个 commit 一个改进点 46 - [ ] 🛑 **GATE**: 全部勾选 = 可以汇报完成。任何一项未勾 = 不能说"done" 47 48 --- 49 50 ## 包含 51 52 | 路径 | 内容 | 53 |---|---| 54 | `references/code-refactor.md` | god-fn 拆分 + 死代码 7 步 + TDD 4 模式 + YAGNI | 55 | `references/code-review-checklist.md` | 合入前机械规则 + no-silent-swallow P0 | 56 | `references/logging-observability.md` | 静默吞错扫描命令 + 修法模板 | 57 | `references/dict-dedup.md` | 字典/常量去重 4 类问题分类 + Phase 0-5 | 58 | `references/dict-dedup-case-levelcfg.md` | _LEVEL_CFG CONFLICT 案例教学 | 59 | `scripts/silent_swallow.py` | 静默吞错扫描器 | 60 | `scripts/reorg_drift.py` | 引用残留扫描(删 module 后检查 caller) | 61 | `mcp_servers/python_refactor_server.py` | MCP server: ruff/vulture/bandit/radon/pyright | 62 63 ## 使用 64 65 ```bash 66 # 静默吞错扫描 67 python ~/.claude/skills/py-improve/scripts/silent_swallow.py src/ 68 69 # 引用残留 70 python ~/.claude/skills/py-improve/scripts/reorg_drift.py 71 72 # MCP 工具(注册到 ~/.claude/settings.json) 73 # 见 mcp_servers/python_refactor_server.py 74 ``` 75 76 ## 14 硬约束(跨子工作流通用) 77 78 1. **零新增依赖 + 零提前防御 (YAGNI)**: 只用 stdlib + 已装库。不为假设风险提前加 try/except / retry / fallback / 抽象层。 79 2. **commit 颗粒度**: 1 逻辑单元 = 1 commit,独立可回滚。 80 3. **默认回滚 = rsync 备份还原** (无损)。**绝对禁止 `git reset --hard`**。 81 4. **死代码证明需 7 步 checklist**: 静态引用 + 文本搜索 + 框架注册 + export + 动态调用 + 测试/生成 + 用户签字。0 caller grep ≠ 证明。 82 5. **TDD 按场景分 4 模式**: Characterization / Red-Green / Structural (7 步 + 全 build/test) / Regression。 83 6. **commit 前全量测试全绿**,不破 CI。**项目级例外**:当本项目跑测试本身不安全时(如测试会改动生产数据),以项目自己的规则为准——在运行报告里记录该例外,并跑能覆盖本次改动的最安全子集。 84 7. **prod 锁定**: 不动线上代码,owner 显式授权才动。 85 8. **宁缺勿伪**: 不确定的事实留 TODO,不编。校验靠 grep / codegraph / pyright 实测。 86 9. **DB 删除必走退场流水线**: DROP 前 RENAME → PLAN_DELETE_<原名> → 7 天测试 → user 审。 87 10. **批量任务先测最小**: 任何批量操作先选最小样本 (1-10) 跑通 + 计时,按比例推全量耗时。**禁止直接开最大集合**。 88 11. **daemon / 服务代码改动 4 步独立**: ①本地 Edit ②本地 py_compile 验证 ③scp 上传 + 清 __pycache__ ④systemctl restart + pgrep 验证 PID 变了。**不链式 restart && smoke**。 89 12. **buffer 所有权被转移**: 缓存方必须保留副本,每次传递前 bytes.slice(0) / np.array(..., copy=True)。 90 13. **hash 化构建产物必须整目录同步**: 前端 chunk 名带 hash — 只推改的文件 → index.html 引用新 hash → 缺 chunk → MIME text/html 404。 91 14. **部署/发布后必须验证实际生效产物标识**: 脚本输出 "✓ done" ≠ 部署成功。 92 93 ## 关联 94 95 - `/repo-medic` — meta 入口 96 - `/doc-reorg` — 重构后目录归档 97 - `/db-tweak` — DB 相关调用方审查 98 99 ## 仓库 100 101 github.com/liyong-labs/repo-medic — Apache-2.0。
liyong-labs/repo-medic/tree/main/docs/zh/py-improve commit 41fa8e2c90
Frequently asked questions How do I install the Py Improve skill? Run npx skillmds@latest add liyong-labs/py-improve-2 in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Py Improve skill do? py-improve — Python 代码改进 It is listed under Coding & Dev Tools on SkillMD.
Is Py Improve safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Py Improve? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Py Improve free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Py Improve? liyong-labs (@liyong-labs) published this skill. Their other Agent Skills are listed on their SkillMD profile.