Get the FREE Ultimate OpenClaw Setup Guide →

python-refactoring

Scanned
npx machina-cli add skill cheickmec/smellcheck/python-refactoring --openclaw
Files (1)
SKILL.md
13.0 KB

Python Refactoring Skill

83 refactoring patterns from Contieri's series, Fowler's catalog, OO metrics literature, and Python idioms.

Modes

Active Analysis (default when given code): Identify smells -> map to patterns -> load relevant references -> apply refactorings with before/after -> note trade-offs.

Reference Lookup (when asked about a pattern by number/name): Load the relevant reference file -> present the pattern.

Smell-to-Pattern Map

Code SmellPatternsFile
Setters / half-built objectsSC101, SC104state.md
Mutable default argsSC701idioms.md
Unprotected public attributesSC103state.md
Magic numbers / constantsSC601hygiene.md
Variables that never changeSC102state.md
Boolean flags for roles/statesSC105state.md
Stale cached/derived attributes030state.md
Long function, multiple concernsSC201, 010functions.md
Comments replacing code005functions.md
Comments explaining what code does011hygiene.md
Generic names (result, data, tmp)SC202functions.md
Duplicated logicSC606hygiene.md
Near-duplicate functions050functions.md
Long related parameter listsSC206, 052functions.md
Query + modify in same functionSC207functions.md
Static functions hiding deps020functions.md
input() in business logicSC203functions.md
Getters exposing data027functions.md
Need to test private methods037functions.md
Unused function parametersSC208functions.md
Long lambda expressionsSC209functions.md
Type-checking if/elif chainsSC302types.md
isinstance() dispatch060idioms.md
Dicts as objects (stringly-typed)012types.md
Raw primitives with implicit rules019, 044types.md
Raw lists, no domain meaning038types.md
Boilerplate __init__/__repr__/__eq__SC304idioms.md
Related functions without a classSC301types.md
Sibling classes with duplicate behavior022types.md
Inheritance without "is-a"023types.md
Constructor needs descriptive name048types.md
Scattered None checks015, SC204types.md
Lazy class (too few methods)SC306types.md
Temporary fields (used in few methods)SC307types.md
Deep nestingSC402control.md
Imperative loops with accumulationSC403control.md
Complex boolean expressionsSC404control.md
Long expressions, no named parts043control.md
Loop doing two things046control.md
Parse/compute/format mixed047control.md
Clunky algorithm049control.md
Boolean flag controlling loopSC405control.md
Complex if/elif dispatch056control.md
Related statements scattered053control.md
Missing default else branchSC407control.md
Complex comprehensionsSC406control.md
Singleton / global stateSC303, SC106architecture.md
Same exception for biz + infra035architecture.md
Chained .attr.attr.attrSC502architecture.md
No fail-fast assertions045architecture.md
Dead code / commented blocksSC401hygiene.md
Unused exceptionsSC602hygiene.md
Empty catch blocksSC605hygiene.md
Giant regex025hygiene.md
Excessive decoratorsSC205hygiene.md
String concatenation for multilineSC603hygiene.md
Inconsistent formatting032hygiene.md
Cryptic error messages031hygiene.md
Sequential IDs leaking infoSC107architecture.md
Error codes instead of exceptionsSC501architecture.md
Blocking calls in async functionsSC703idioms.md
Manual try/finally cleanupSC702, SC604idioms.md
Full lists when streaming works059idioms.md
Indexing tuples by positionSC305idioms.md
Shotgun surgery (wide call spread)SC505architecture.md
Deep inheritance treeSC308types.md
Wide hierarchy (too many subclasses)SC309types.md
Inappropriate intimacySC506architecture.md
Speculative generality (unused ABCs)SC507architecture.md
Unstable dependencySC508architecture.md
Low class cohesion (LCOM)SC801metrics.md
High coupling between objects (CBO)SC802metrics.md
Excessive fan-outSC803metrics.md
High response for class (RFC)SC804metrics.md
Middle man (excessive delegation)SC805metrics.md

Reference Files

Load only the file(s) matching detected smells:

  • references/state.md -- Immutability, setters, attributes (SC101, SC102, SC103, SC104, SC105, 030)
  • references/functions.md -- Method extraction, naming, parameters, CQS (SC201, 005, SC202, 010, 020, SC203, 027, SC206, 037, SC207, 050, 052, SC208, SC209)
  • references/types.md -- Class design, reification, polymorphism, nulls (SC301, 012, SC302, 015, 019, 022, 023, SC204, 038, 044, 048, SC306, SC307, SC308, SC309)
  • references/control.md -- Guard clauses, pipelines, conditionals, phases (SC402-SC404, 043, 046, 047, 049, 053, SC405, 056, SC406, SC407)
  • references/architecture.md -- DI, singletons, exceptions, delegates (SC303, SC106, SC107, 035, 045, SC501, SC502, SC505, SC506, SC507, SC508)
  • references/hygiene.md -- Constants, dead code, comments, style (SC601, SC602, 011, SC606, SC401, 025, 031-032, SC205, SC603, SC605)
  • references/idioms.md -- Context managers, generators, unpacking, protocols, async (SC701, SC702, SC703, 059, 060, SC304, SC305, SC604)
  • references/metrics.md -- OO metrics: cohesion, coupling, fan-out, response, delegation (SC801, SC802, SC803, SC804, SC805)

Automated Smell Detector

scripts/detect_smells.py runs the smell detector with 56 automated checks (41 per-file + 10 cross-file + 5 OO metrics). It ships with a bundled copy of the smellcheck package — no pip install required.

# Works immediately after skill install — no pip required
python3 scripts/detect_smells.py src/
python3 scripts/detect_smells.py myfile.py --format json

# Look up rule documentation (description + before/after example)
python3 scripts/detect_smells.py --explain SC701
python3 scripts/detect_smells.py --explain SC4    # list a family
python3 scripts/detect_smells.py --explain all    # list all rules

# Caching (enabled by default — skips unchanged files on repeat scans)
python3 scripts/detect_smells.py src/ --no-cache       # fresh scan
python3 scripts/detect_smells.py --clear-cache         # purge cache

# Or use the pip-installed CLI directly
pip install smellcheck
smellcheck src/ --min-severity warning --fail-on warning

Per-file detections (41): SC101 setters, SC201 long functions, SC601 magic numbers, SC602 bare except, SC202 generic names, SC301 extract class, SC102 UPPER_CASE without Final, SC103 public attrs, SC302 isinstance chains, SC104 half-built objects, SC105 boolean flags, SC303 singleton, SC401 dead code after return, SC106 global mutables, SC203 input() in logic, SC107 sequential IDs, SC204 return None|list, SC205 excessive decorators, SC206 too many params, SC603 string concatenation, SC402 deep nesting, SC403 loop+append, SC207 CQS violation, SC404 complex booleans, SC501 error codes, SC502 Law of Demeter, SC405 control flags, SC701 mutable defaults, SC702 open without with, SC703 blocking calls in async, SC304 dataclass candidate, SC305 sequential indexing, SC604 contextlib candidate, SC210 cyclomatic complexity, SC208 unused parameters, SC605 empty catch block, SC209 long lambda, SC406 complex comprehension, SC407 missing else, SC306 lazy class, SC307 temporary field.

Cross-file detections (10): SC606 duplicate functions (AST-normalized hashing), SC503 cyclic imports (DFS), SC504 god modules, SC211 feature envy, SC505 shotgun surgery, SC308 deep inheritance, SC309 wide hierarchy, SC506 inappropriate intimacy, SC507 speculative generality, SC508 unstable dependency.

OO metrics (5): SC801 lack of cohesion, SC802 coupling between objects, SC803 fan-out, SC804 response for class, SC805 middle man.

Run the detector first for a quick scan, then use the reference files to understand and apply the suggested refactorings.

Guided Refactoring Workflow

smellcheck groups its 56 rules into 9 ordered phases with dependency chains. The --plan command generates a customized refactoring plan based on actual findings.

Initialization

# Bundled (no pip install required)
python3 scripts/detect_smells.py <path> --plan
python3 scripts/detect_smells.py <path> --plan --format json

# Or via pip-installed CLI
smellcheck <path> --plan
smellcheck <path> --plan --format json

The plan auto-selects a strategy: local_first (default, phases 0-8) or architecture_first (reorders to [0,1,7,8,2,3,4,5,6]) when >30% of findings are cross-file or metric scoped.

Phase Execution Loop

Each phase has three passes:

Pass 1 — Automated scan: Run the --select command printed in the plan output for the current phase. Fix each finding using the reference file for that rule's family.

Pass 2 — Agent semantic review: For candidate files (files with automated findings + changed files in branch + import neighbors for cross-file phases), check for manual patterns assigned to the current phase. Evidence requirements for every semantic finding:

  • File path and line range
  • Actual code snippet
  • Pattern number and name
  • Why it matches the heuristic
  • Confidence score (>=0.8 auto-fix, 0.5-0.79 suggest+confirm, <0.5 don't emit)

Pass 3 — Gate check: If gate is tests_pass, run the test suite and do not proceed until green. If rescan_after is true, re-run --plan and re-bucket.

Manual Pattern Assignments per Phase

For each manual pattern below, load the corresponding reference file from the Reference Files section to see full before/after examples.

PhasePatternsAgent detection heuristics
0 correctness(none)Fully automated
1 dead_code005Comments that restate the next line — delete them
2 clarity011, 031011: comments explaining "what" not "why". 031: error messages missing context
3 idioms059, 060059: list(gen) where result is only iterated once. 060: single isinstance() dispatch
4 control_flow043, 046, 047, 049, 053, 056043: expression >80 chars with no named subparts. 046: loop body with 2+ concerns. 047: function that parses+computes+formats. 049: manual impl of stdlib op. 053: related lines separated by unrelated. 056: if/elif 4+ branches
5 functions010, 020, 027, 037, 050, 052020: function-scoped import. 027: obj.get_x() + logic → move to obj. 037: test accessing ._private. 050: two functions >80% body similarity. 052: 3+ related params
6 state_class012, 019, 022, 023, 030, 038, 044, 048012: dict with 3+ consistent keys → dataclass. 019/044: primitive with constraints → value object. 022: siblings with duplicate methods. 023: inheritance for reuse not "is-a". 030: cached attr that drifts. 038: list[str] with domain meaning. 048: unclear constructor → factory
7 architecture015, 025, 035, 045015: if x is not None in 3+ sites → Null Object. 025: regex >80 chars → re.VERBOSE. 035: same exception for biz+infra. 045: broad input without precondition
8 metrics(none)Fully automated; use results to revisit phase 6

Pattern 032 (inconsistent formatting): Tool-enforced — run black or ruff format before starting workflow.

Conditional Branching

  • No classes detected → phases 6 and 8 skip automatically
  • No async code (SC703 has 0 findings) → skip in phase 3
  • Architecture-dominant (>30% cross-file) → plan reorders to architecture-first
  • Single-file scan → cross-file phases will naturally have 0 findings
  • Large backlog → generate baseline, gate only new findings

Feedback Loops

  • Phase 8 → Phase 6: under local_first, if metrics still high, loop back (max 2 iterations). Under architecture_first, phase 8 runs before phase 6, so this becomes forward flow — metrics inform the class design pass.
  • Phase 5 → Phase 8: extraction changes cohesion, re-check metrics
  • Any phase → tests: if tests break, pause and fix first

Completion Criteria

All phases either skip (0 findings) or pass. Tests green. Feedback loops converged. smellcheck --plan shows all phases as skip.

Guidelines

  • Prioritize structural refactorings over cosmetic
  • Preserve existing tests -- refactoring changes structure, not behavior
  • Group related changes when multiple patterns apply
  • Flag changes to public API (breaking vs non-breaking)
  • For large code, suggest incremental order rather than all-at-once
  • When patterns conflict, explain the trade-off and recommend

Source

git clone https://github.com/cheickmec/smellcheck/blob/main/plugins/python-refactoring/skills/python-refactoring/SKILL.mdView on GitHub

Overview

This skill provides a comprehensive catalog of 83 Python refactoring patterns drawn from Contieri's series, Fowler's catalog, OO metrics literature, and Python idioms. It analyzes code for smells, maps them to numbered refactoring patterns, and presents before/after examples to guide cleanups. Use it when you want structured, reference-driven Python code quality improvements, especially when you invoke /refactor or explicitly ask to refactor.

How This Skill Works

In Active Analysis mode, it identifies smells, maps them to relevant refactoring patterns (e.g., long functions, mutable defaults, unprotected public attributes, dead code, deep nesting), loads the pattern references, and applies refactorings with before/after illustrations while noting trade-offs. For pattern lookup, Reference Lookup loads the exact pattern doc and presents it. Refactoring proceeds in measurable steps with test validation and clear trade-offs.

When to Use It

  • You have a long function that does many things and needs decomposition.
  • You encounter mutable default arguments or public attributes that expose internal state.
  • You want to remove dead code or comments that merely describe what the code does.
  • Your control flow has deep nesting or complex boolean expressions that hinder readability.
  • You aim to reorganize related functions into a class or cleaner architecture to reduce duplication.

Quick Start

  1. Step 1: Run Active Analysis on the target Python module to identify smells and candidate patterns.
  2. Step 2: Map smells to numbered refactoring patterns and review the before/after examples.
  3. Step 3: Apply selected refactorings, commit with rationale, and run tests to verify behavior.

Best Practices

  • Start with a smells audit and map each smell to a numbered refactoring pattern before changing code.
  • Tackle refactors in small, testable steps and continuously run tests to catch regressions.
  • Consult the pattern references (state.md, functions.md, idioms.md, control.md, architecture.md, hygiene.md, types.md) for concrete before/after guidance.
  • Preserve behavior; compare test outcomes and document trade-offs in commit messages.
  • Use incremental commits with clear, descriptive messages that reference the specific pattern applied.

Example Use Cases

  • Split a multi-hundred-line function into smaller helpers with descriptive names to improve readability and testability.
  • Replace a mutable default argument with None and initialize inside the function to avoid shared state issues.
  • Encapsulate unprotected public attributes behind properties to enforce invariants and validation.
  • Remove dead code blocks and stop using comments that replace code with misleading explanations.
  • Refactor related functions into a dedicated class (or composition) to reduce duplication and improve cohesion.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers