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/anvil-toolset 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
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
Coordinate parallel code reviews across multiple quality dimensions with finding deduplication, severity calibration, and consolidated reporting. Use this skill when organizing multi-reviewer code reviews, calibrating finding severity, or consolidating review results.
tools
Knowledge base for the codebase-mapper plugin. Provides writing guidelines, tone rules, and diagram conventions for generating human-readable project guides. Referenced by all codebase-mapper agents during document generation. TRIGGER WHEN: referenced by codebase-mapper pipeline agents (codebase-explorer, overview-writer, tech-writer, flow-writer, onboarding-writer, ops-writer, config-writer, guide-reviewer) during document generation. DO NOT TRIGGER WHEN: outside the /map-codebase pipeline (general documentation work should use docs:readme-craft or codebase-mapper:docs-create).
tools
Progressive Web App knowledge base for 2025-2026: Web App Manifest, Service Workers (Workbox 7, Serwist), Web Push (VAPID, RFC 8030/8291/8292, Declarative Push for Safari 18.4+), install flows (beforeinstallprompt, Window Controls Overlay), OPFS storage, Project Fugu, Core Web Vitals (INP < 200ms), security (HTTPS, CSP, COOP/COEP), and distribution (Bubblewrap, PWA Builder MSIX, Capacitor). TRIGGER WHEN: building, auditing, or debugging PWAs, including manifest, service worker, Web Push, install flow, OPFS, Background Sync, Wake Lock, vite-plugin-pwa, Next.js Serwist, @angular/pwa, @vite-pwa/nuxt, Bubblewrap, TWA, PWA Builder, or Capacitor wrapping. DO NOT TRIGGER WHEN: the task is generic frontend styling (use frontend), React performance (use react-development:review-react), cross-platform security unrelated to PWA (use platform-engineering), Tauri or Electron wrappers (use tauri-development), or GA4 / analytics (use digital-marketing).