python-refactor

python-refactor is a command for Claude Code from acaprino/daodan. It costs 80 tokens per session (1,747 once invoked), scanned A, original, MIT.

A controlled workflow for refactoring Python code, with measured analysis, an approval checkpoint, one change at a time, and test validation. Refactoring changes code structure without changing its intended behavior; Python is a programming language.

In plain words
What is it for?
Use it to refactor Python modules, measure complexity before and after, apply approved changes, run tests, and report the result.
Why use it?
It provides a recorded process for reducing complexity or restructuring modules while preventing changes from continuing after failed tests or without approval.

Command for Claude Code

Written for Claude Code: argument-hint in frontmatter.

Runs only inside its plugin — its command needs a path that Claude Code sets for a plugin’s own hooks and for nothing else. Install the plugin, not this.

Part of the python-development plugin — 9 skills, 3 commands, 3 agents shipped together

Good fit Use it to refactor Python modules, measure complexity before and after, apply…

Compare 6 commands from other repositories ↓
Install

Getting it into your agent

This one installs as part of its plugin. Adding the marketplace and installing the plugin brings it with everything else the plugin ships.

Claude Code
/plugin marketplace add acaprino/daodan
Claude Code
/plugin install python-development

Made for: Claude Code.

Or install python-development, the plugin that ships this one along with the rest of its 9 skills, 3 commands, 3 agents.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for python-refactor

README.md
[![agentmods](https://agentmods.dev/badge/commands/acaprino/daodan/python-refactor.svg)](https://agentmods.dev/commands/acaprino/daodan/python-refactor)
Your own site
<a href="https://agentmods.dev/commands/acaprino/daodan/python-refactor"><img src="https://agentmods.dev/badge/commands/acaprino/daodan/python-refactor.svg" alt="Measured on agentmods" height="20"></a>
Per session 80 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,747 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00080 $0.01747
Opus 5 $0.00040 $0.00873
Sonnet 5 $0.00016 $0.00349
Haiku 4.5 $0.00008 $0.00175

Measured yesterday against content hash 2f70a940bf0c, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-06, from the pricing page.

Security

Grade A, and why

python-refactor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

exports/claude/plugins/python-development/commands/python-refactor.md · 250 lines

How it starts

The opening of the file, as written. The whole thing — 250 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Python Refactoring

CRITICAL RULES

  1. Execute phases in order. Analysis -> Planning -> Approval -> Execution -> Validation.
  2. Write output files. Each phase writes to .python-refactor/ for persistence and context.
  3. Stop at checkpoint. Get user approval of the refactoring plan BEFORE making any code changes.
  4. Run tests after every change. Never proceed if tests fail.
  5. Never enter plan mode. Execute immediately.
  6. One pattern at a time. Don't combine multiple refactorings in a single edit.

Pre-flight

1. Check for existing session

Check if .python-refactor/state.json exists:

  • If in progress: offer to resume or start fresh
  • If complete: offer to archive and start fresh

2. Initialize state

Create .python-refactor/ directory and state.json:

{
  "target": "$ARGUMENTS",
  "status": "in_progress",
  "flags": {
    "strict_mode": false
  },
  "current_phase": 1,
  "completed_phases": [],
  "files_created": [],
  "started_at": "ISO_TIMESTAMP"
}

Phase 1: Analysis

  1. Read the target code to understand current structure

  2. Run complexity metrics (if available):

    uv run python ${CLAUDE_PLUGIN_ROOT}/skills/python-refactor/scripts/measure_complexity.py $ARGUMENTS 2>/dev/null
    uv run python ${CLAUDE_PLUGIN_ROOT}/skills/python-refactor/scripts/analyze_with_flake8.py $ARGUMENTS 2>/dev/null
    

    If scripts aren't available, analyze manually by reading the code.

  3. Identify issues against thresholds:

    • Cyclomatic complexity: >10 per function (high priority)
    • Cognitive complexity: >15 per function (high priority)
    • Function length: >30 lines (medium priority)
    • Nesting depth: >3 levels (medium priority)
  4. Establish test baseline:

    pytest [test files] -v 2>/dev/null || python -m pytest -v 2>/dev/null
    

    Record which tests exist and their pass/fail status.

Output file: .python-refactor/01-analysis.md

# Phase 1: Analysis

## Target
[file/directory path]

## Current Metrics
| Metric | Value | Threshold | Status |
|--------|-------|-----------|--------|
| Cyclomatic Complexity (avg) | X | <10 | PASS/FAIL |
| Cognitive Complexity (max) | X | <15 | PASS/FAIL |
| Max Function Length | X lines | <30 | PASS/FAIL |
| Max Nesting Depth | X | <=3 | PASS/FAIL |

## Issues Found
[Ordered by severity: HIGH, MEDIUM, LOW]

## Test Baseline
- Tests found: [count]
- Passing: [count]
- Failing: [count]

Read the full file on GitHub · 250 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 250 lines · 80 tokens per session scan A 2f70a940bf0c

Subscribe to this mod's changes

python-refactor is a command published in the GitHub repository acaprino/daodan (8 stars, last pushed yesterday), licensed MIT. It adds 80 tokens to every session and 1,747 once invoked, about $0.0004 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-05.