Python Refactoring logo

Python Refactoring

Organization
Ascend
python-refactoring

Python 代码重构技能,覆盖代码坏味道识别、设计模式应用、可读性改进和实战经验。当用户要求"重构代码"、"refactor"、"代码优化"、"改善代码质量"、"code smell review"、"应用设计模式"、"提升可读性",或提交代码审查请求时使用此技能。支持在重构完成后输出结构化重构文档("输出重构文档"、"生成重构报告")。包含基于 vllm-ascend 仓库 20+ 个真实重构 PR 提炼的实战模式。

Overview

PublisherAscend
Repositoryagent-skills
Skill namepython-refactoring
Stars
54
Forks
4
Bundled files
6
LicenseMulanPSL-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 6 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by Ascend on GitHub. Read the source before you install it.

Installation

Install the Python Refactoring AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/Ascend/agent-skills/tree/master/skills/python-refactoring
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Ascend/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/python-refactoring .claude/skills/python-refactoring
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Python Refactoring in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Python Refactoring on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Python Refactoring is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Python 代码重构

对 Python 代码进行系统性重构,在不改变外部行为的前提下改善内部结构,提升可读性、可维护性和可扩展性。

Overview

  • 适用场景:代码重构、代码审查、代码质量改善、技术债务清理
  • 核心原则:重构 ≠ 重写;保持接口兼容;有测试才重构;最小改动
  • 参考文档:基于 vllm-ascend 仓库 20+ 个真实重构 PR 提炼
  • 约束配置:所有数值阈值(函数行数、参数个数等)集中在 refactoring-config.json,用户可自定义或禁用

Quick Reference

任务参考文档
自定义约束阈值refactoring-config.json
识别代码问题references/code-smells.md
选择设计模式references/design-patterns.md
改善可读性references/readability.md
实战模式与案例references/refactoring-in-practice.md
生成重构文档references/refactoring-report.md

坏味道 → 重构模式 速查表

发现问题后,用此表快速定位解决方案:

坏味道推荐模式详细参考
过长函数Extract Method、Guard Clausecode-smells.md §1.1
过长参数列表dataclass 参数对象、Builderdesign-patterns.md §1.3
重复代码(横切关注点)装饰器模式design-patterns.md §2.1
重复代码(流程骨架相同)模板方法 / 策略模式practice §模式三
上帝类按职责拆分、组合替代继承practice §模式二、五
过度继承组合 + Protocoldesign-patterns.md §3.1
if-elif 创建实例工厂模式(注册表)practice §模式一
if-elif 类型分发多态 / singledispatchdesign-patterns.md §3.4
多个 xxx_with_yyy 函数变体策略模式(Protocol)practice §模式三
dict/tuple 传递复合数据dataclass 类型形式化practice §模式七
魔法数字/字符串枚举(Enum / IntEnum)code-smells.md §3.3
自定义实现与上游重复对齐上游 / 继承基类practice §模式四
过大模块(>500 行)分离关注点、包化practice §模式二
基准测试证明的劣势路径删除死路径practice §模式六

重构执行流程

┌─────────────────────────────────────────────────────────┐
│  0. 前置检查                                              │
│     - 确认有测试覆盖(无则先补关键路径测试)                  │
│     - 确认代码可正常运行                                    │
│     - 评估重构规模 → refactoring-in-practice.md 规模矩阵   │
├─────────────────────────────────────────────────────────┤
│  1. 识别问题  →  references/code-smells.md               │
│     - 通读代码,按分类逐项检查                               │
│     - 输出问题清单,按严重程度排序                            │
├─────────────────────────────────────────────────────────┤
│  2. 选择策略  →  references/refactoring-in-practice.md   │
│     - 匹配七大实战模式,选择最合适的重构路径                  │
│     - 大型重构制定分阶段计划(参考 Quantization/MoE 案例)    │
├─────────────────────────────────────────────────────────┤
│  3. 结构重构  →  references/design-patterns.md           │
│     - 对结构性问题选择合适的模式                             │
│     - 实施重构,确保接口兼容                                 │
├─────────────────────────────────────────────────────────┤
│  4. 打磨优化  →  references/readability.md               │
│     - 改善命名、结构、类型标注                               │
│     - 补充必要注释和文档                                    │
├─────────────────────────────────────────────────────────┤
│  5. 验证                                                 │
│     - 运行全部已有测试,确认无回归                            │
│     - 运行 linter / type checker                          │
│     - 对比重构前后,确认行为一致                             │
├─────────────────────────────────────────────────────────┤
│  6. 输出重构文档(可选,用户请求时生成)                       │
│     - 汇总本次重构的完整记录                                 │
│     - 生成结构化文档,可用于 PR 描述 / 团队分享 / 项目归档     │
│     - 参见 references/refactoring-report.md                  │
└─────────────────────────────────────────────────────────┘

重构决策指南

何时重构

  • 添加新功能前,先重构相关代码使其易于扩展
  • 修复 Bug 时,顺便改善周边代码结构
  • Code Review 中发现的问题
  • 代码难以理解或修改时

何时不重构

  • 即将废弃的代码
  • 没有测试覆盖且无法快速补充测试的核心代码
  • 时间紧迫的紧急修复(先修复,后重构)
  • 代码虽不完美但足够清晰且稳定运行

重构粒度控制

规模范围建议参考案例
小型单个函数/方法直接修改,单次提交MoE #5189 复用上游 all_reduce(-38 行)
中型单个类或模块拆分为 2-3 次提交MoE #5481 dict/tuple → dataclass(6 文件)
大型跨模块/包制定计划,分阶段执行Quantization 4 阶段重构(36 文件)

重构文档模板

当用户请求生成重构文档(如 "输出重构文档"、"生成重构报告"、"写重构总结")时,在重构完成并验证后,按 references/refactoring-report.md 中的模板生成文档。

文档支持按用途裁剪(PR 描述 / 团队分享 / 项目归档),小型重构可使用精简格式。详见参考文档。


输出格式

对每个重构建议,按以下格式输出:

### [问题类型] 问题简述

**位置:** `file_path:line_number`
**问题:** 描述当前代码的问题
**方案:** 描述修复方案
**实战参考:** 匹配的实战模式(如适用)
**修复前:**
(代码片段)
**修复后:**
(代码片段)
**依据:** 引用的参考文档和具体规则

References

详细参考文档:

  • code-smells.md — 代码坏味道识别与修复(函数/类/逻辑/模块四个层级,含 isinstance 链分发)
  • design-patterns.md — Pythonic 设计模式应用(创建型/结构型/行为型 + singledispatch + 反模式警示)
  • readability.md — 可读性与可维护性改进(命名/结构/类型标注/注释/模块组织/Pythonic 惯用法)
  • refactoring-in-practice.md — 基于 vllm-ascend 真实 PR 的重构实战经验(七大模式 + 安全守则 + 规模评估矩阵)
  • refactoring-report.md — 重构文档生成(模板 + 各节填写指南 + 场景适配 + 小型重构精简格式)

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Python Refactoring AI skill do?

Python 代码重构技能,覆盖代码坏味道识别、设计模式应用、可读性改进和实战经验。当用户要求"重构代码"、"refactor"、"代码优化"、"改善代码质量"、"code smell review"、"应用设计模式"、"提升可读性",或提交代码审查请求时使用此技能。支持在重构完成后输出结构化重构文档("输出重构文档"、"生成重构报告")。包含基于 vllm-ascend 仓库 20+ 个真实重构 PR 提炼的实战模式。

Why use Python Refactoring on TypingMind?

Because you install it once and use it with any model. Python Refactoring is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Python Refactoring in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/Ascend/agent-skills/tree/master/skills/python-refactoring. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Python Refactoring?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Python Refactoring?

As many as you like. As long as a model supports skills, you can use Python Refactoring with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Python Refactoring AI skill free?

Yes. It is published on GitHub by Ascend under the MulanPSL-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇