kata-python-refactor

Add characterization tests before refactoring Python code with incremental commits.

Updated Sep 3, 2025
One-click install
npx skills add https://github.com/guardiatechnology/design-system --skill kata-python-refactor
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kata-python-refactor
Source: https://github.com/guardiatechnology/design-system/tree/main/.claude/skills/kata-python-refactor
Command: npx skills add https://github.com/guardiatechnology/design-system --skill kata-python-refactor

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps you refactor existing Python code without accidentally changing behavior, by using test coverage and incremental, verifiable transformations.

Core Features & Use Cases

  • Assess and protect behavior with coverage: confirm what is currently tested and add missing characterization tests before making changes.
  • Plan risk-minimized refactoring steps: split work into small, independently committable transformations that can be validated by running the suite.
  • Execute with continuous verification: enforce tight feedback loops using Ruff, Black/format checks, mypy, and pytest after each step.
  • Use Case: when a module is hard to maintain (e.g., a “god class” or entangled infrastructure/domain logic), you can progressively rename, extract, and reorganize while keeping endpoints and outputs unchanged.

Quick Start

Run the test suite for the target module with coverage, add characterization tests if needed, then apply small refactoring steps while verifying with ruff, mypy, and pytest after each commit.

Frequently Asked Questions about kata-python-refactor

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I safely refactor Python code without changing behavior?

To safely refactor Python code, you must validate current behavior by running test coverage and adding missing characterization tests first. You then apply incremental transformations while verifying Ruff, mypy, and pytest between commits to ensure no unintended changes occur.

What are characterization tests and when do I need them for Python refactoring?

Characterization tests capture the current behavior of your Python code before you refactor. You need them when existing test coverage is insufficient, ensuring you have a safety net to detect unintended changes when reorganizing entangled modules like god classes.

How do I split a large Python refactoring task into smaller steps?

You split Python refactoring tasks into small, independently committable transformations that can each be validated by running the test suite. This risk-minimized planning ensures behavior and interface changes never occur in the same commit, keeping endpoints stable.

Can I use ruff and mypy to verify Python refactoring steps?

Yes, you can and should use Ruff for linting and formatting, and mypy for type checking to verify Python refactoring steps. Running these alongside full pytest checks after each commit enforces a tight feedback loop to catch issues early.

What is the best way to refactor a god class in Python?

The best way to refactor a Python god class is to progressively rename, extract, and reorganize the entangled infrastructure and domain logic. This uses continuous verification through tests and linting to keep endpoints and outputs unchanged throughout the process.

Why should behavior and interface changes not occur in the same commit during Python refactoring?

Behavior and interface changes must not occur in the same commit to maintain a verifiable safety net. Separating them ensures that each incremental transformation is purely structural, allowing test suite validation to isolate whether the refactor preserved existing outputs.