/pythonista:python-refactorfrom pythonista
Restructure tangled code into a clear equivalent, preserving behavior. TRIGGER WHEN: the user asks for "readable", "maintainable" or "clean" code, a review flags comprehension issues, or the task is legacy modernization or onboarding cleanup.
Ships in pythonista. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Transform complex Python into clear, maintainable code while preserving correctness. Phased workflow with safety-by-design and continuous validation. For deep references (anti-patterns, OOP principles, cognitive complexity, regression prevention), see the references/ directory.
global statements, no clear module/class organization, configuration mixed with business logicflake8 --select=F821,E0602 (or ruff check --select=F821) BEFORE tests. Catches NameErrors immediately.Refactoring must NEVER introduce regressions. Read references/REGRESSION_PREVENTION.md before any session.
Before each session:
After EACH micro-change (not at the end -- every single one):
flake8 --select=F821,E999 → 0 errorspytest -x → all passingIf ANY check fails: STOP → REVERT → ANALYZE → FIX APPROACH → RETRY.
ANY REGRESSION = TOTAL FAILURE.
references/anti-patterns.md (script-like/global-state, God Objects, nested conditionals, long functions, magic numbers, cryptic names).references/oop_principles.md (proper classes/modules, encapsulated state, separated responsibilities, SOLID, DI vs hard-coded deps).python-scan's analyze_multi_metrics.py (../python-scan/scripts/analyze_multi_metrics.py <path> --format json).assets/templates/analysis_template.md.Output: prioritized list of issues by impact and risk.
Output: refactoring plan, sequenced changes, migration plans, test strategy, rollback plan.
references/examples/script_to_oop_transformation.md)flake8 <file> --select=F821,E0602 # undefined names/variables -- MUST be 0
flake8 <file> --select=F401 # unused imports
flake8 <file> # full quality check
scripts/compare_metrics.py <before_file> <after_file> (shells out to python-scan's complexity and documentation analyzers).scripts/benchmark_changes.py for hot paths.assets/templates/summary_template.md.Structural moves only -- full catalog with examples in references/patterns.md. Idiom-level patterns (guard clauses, dictionary dispatch, match statements, named boolean conditions) live in python-patterns instead of being re-described here.
For cognitive complexity calculation rules and reduction strategies, see references/cognitive_complexity_guide.md.
is_active / has_permission / can_edit, collections as pluralscalculate_total, validate_email); boolean queries as is_valid() / has_items()UPPERCASE_WITH_UNDERSCORES; replace magic numbers/stringsUserAccount, PaymentProcessor)Full catalog: references/anti-patterns.md.
uv tool install ruff complexipy radon
ruff check src/ # fast linting (Rust, replaces flake8+plugins)
complexipy src/ --max-complexity-allowed 15 # cognitive complexity (Rust)
radon mi src/ -s # maintainability index
Full configuration (pyproject.toml, pre-commit, GitHub Actions): references/cognitive_complexity_guide.md.
For projects already on flake8, see references/flake8_plugins_guide.md (curated 16-plugin selector list).
python-scan's analyze_multi_metrics.py combines complexipy + radon + maintainability index in a single report (../python-scan/scripts/analyze_multi_metrics.py <path> --format json). This skill's scripts/compare_metrics.py shells out to it (and to python-scan's check_documentation.py) to diff before/after.
| Metric | Tool | Use |
|---|---|---|
| Cognitive complexity | complexipy | Human comprehension |
| Cyclomatic complexity | ruff (C901), radon | Test planning |
| Maintainability index | radon | Overall code health |
Full guide: references/REGRESSION_PREVENTION.md. Key traps:
Escalation path: python-patterns → python-refactor (idiom-level to structural).
scripts/compare_metrics.py shell out to its analyzersuv run ruff, uv run complexipy for tool executionPerf-critical optimized code (profile first), code scheduled for deletion, external deps (contribute upstream), stable legacy code nobody needs to modify.
Cannot improve algorithmic complexity (that's an algorithm change). Cannot add domain knowledge not in code/comments. Cannot guarantee correctness without tests. Style preferences vary -- adjust to team conventions.
references/examples/:
script_to_oop_transformation.md -- script → clean OOP architecture (flagship case study)python_complexity_reduction.md -- nested conditionals and long functionstypescript_naming_improvements.md -- naming patterns (cross-language reference)assets/pyproject.toml (opens in a new tab)assets/templates/analysis_template.md (opens in a new tab)assets/templates/flake8_report_template.md (opens in a new tab)assets/templates/summary_template.md (opens in a new tab)references/REGRESSION_PREVENTION.md (opens in a new tab)references/anti-patterns.md (opens in a new tab)references/cognitive_complexity_guide.md (opens in a new tab)references/examples/python_complexity_reduction.md (opens in a new tab)references/examples/script_to_oop_transformation.md (opens in a new tab)references/examples/typescript_naming_improvements.md (opens in a new tab)references/flake8_plugins_guide.md (opens in a new tab)references/oop_principles.md (opens in a new tab)references/patterns.md (opens in a new tab)scripts/analyze_with_flake8.py (opens in a new tab)scripts/benchmark_changes.py (opens in a new tab)scripts/compare_flake8_reports.py (opens in a new tab)scripts/compare_metrics.py (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub takfes/indie-marketplace --plugin pythonista12 skills in this plugin
/python-architectureWhole-module structural critique using deep-module design vocabulary: is a module's interface small relative to what it hides (deep) or nearly as complex as its implementation (shallow), does it survive the deletion test, and does its seam sit in the right place. Emits structured findings, never an HTML report or a mutation. Explicit call only, or reachable through python-workflow's critique recipe recommending it by name.
/python-documentAdd missing module summaries, docstrings, type annotations, and why-comments to Python code -- additive only, never changing behavior or rewording existing documentation. Use when the user asks to document, add docstrings/type hints/comments to, or improve documentation coverage of Python code, or as the documentation stage of a ship pipeline./python-