plugins/python-development/skills/python-refactor/SKILL.md
Systematic code refactoring skill that transforms complex, hard-to-understand code into clear, well-documented, maintainable code while preserving correctness. Applies structured refactoring patterns with validation. TRIGGER WHEN: users request "readable", "maintainable", or "clean" code, during code reviews flagging comprehension issues, for legacy code modernization, or in educational/onboarding contexts DO NOT TRIGGER WHEN: the task is outside the specific scope of this component.
npx skillsauth add acaprino/alfio-claude-plugins python-refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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).scripts/measure_complexity.py or scripts/analyze_multi_metrics.py.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/measure_complexity.py or scripts/analyze_multi_metrics.py.scripts/benchmark_changes.py for hot paths.assets/templates/summary_template.md.Full catalog with examples in references/patterns.md. Key patterns:
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 wily
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).
scripts/analyze_multi_metrics.py combines complexipy + radon + maintainability index in a single report.
| Metric | Tool | Use | |--------|------|-----| | Cognitive complexity | complexipy | Human comprehension | | Cyclomatic complexity | ruff (C901), radon | Test planning | | Maintainability index | radon | Overall code health |
Trends matter, not just thresholds. Setup + CI integration: references/cognitive_complexity_guide.md.
Full guide: references/REGRESSION_PREVENTION.md. Key traps:
Escalation path: clean-code → python-refactor (safest to most thorough).
uv 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)development
Quality gates for multi-reviewer code review pipelines: adversarial verification panel, completeness critic, reviewer pipeline conventions, and the context sharing pattern for parallel reviewers. TRIGGER WHEN: running /senior-review:team-review quality gates; running /senior-review:code-review Steps 4b/4c (adversarial verification and completeness check); consolidating or deduplicating findings from multiple parallel reviewers. DO NOT TRIGGER WHEN: single-reviewer style review without a consolidation phase, or generic team coordination (the upstream agent-teams skills cover that).
development
Knowledge base for pure-architecture decisions on when to unify duplicated logic into a shared abstraction versus leave it duplicated. Covers the canonical theory (Rule of Three, DRY/WET/AHA, Wrong Abstraction, Locality of Behaviour, Bounded Contexts, Tidy First options framing, CUPID vs SOLID), 12 essential-duplication patterns that justify unification, 12 wrong-abstraction patterns that justify inlining or decomposition, an operational decision frame, and a verified reading list. TRIGGER WHEN: the user is making an architectural decision about whether to centralize, extract, or remove a layer; reviewing an abstraction for premature generality; auditing scattered cross-cutting concerns; spawned by the abstraction-architect agent during /abstraction-architect:audit or as the Abstraction dimension of /senior-review:team-review or /senior-review:code-review; the user asks "should I extract this into a service" / "is this DRY enough" / "is this wrong abstraction". DO NOT TRIGGER WHEN: the task is code formatting and readability cleanup (use clean-code:clean-code), Python-specific refactoring with metrics (use python-development:python-refactor), generic dead-code removal (use senior-review:cleanup-dead-code), security review (use senior-review:security-auditor), or pure pattern-consistency review without an architecture lens (use senior-review:code-auditor).
development
Unified web frontend knowledge base covering CSS architecture, UX psychology, UI components, distinctive aesthetics, and interface design generation. TRIGGER WHEN: working on web styling, design systems, component decisions, responsive strategy, distinctive frontend aesthetics, or exploring multiple interface designs. DO NOT TRIGGER WHEN: the task is purely backend or unrelated to web frontend.
development
Stripe payments knowledge base - API patterns, checkout optimization, subscription lifecycle, pricing strategies, webhook reliability, Firebase integration, cost analysis, and revenue modeling. Loaded by stripe-integrator and revenue-optimizer agents; also consumable directly when the user asks for Stripe-specific patterns without needing an agent. TRIGGER WHEN: working with Stripe API (Payment Intents, Customers, Subscriptions, Checkout Sessions, Connect, webhooks, tax, usage-based billing), pricing strategy, or revenue modeling. DO NOT TRIGGER WHEN: payment work is non-Stripe (PayPal, Square, crypto) or the task is generic e-commerce unrelated to payments.