agentsclimarketplace

Python refactoring

Skill cheickmec/smellcheck/plugins/python-refactoring/skills/python-refactoring

Python code smell detector & refactoring guide — 82 patterns, 55 AST checks, zero dependencies. Works as Agent Skills plugin, PyPI package, GitHub Action, or pre-commit hook.

Install
npx -y skills add cheickmec/smellcheck --skill python-refactoring

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

Python refactoring catalog with 83 patterns covering immutability, class design, control flow, architecture, OO metrics, and Python idioms. Analyze code for smells and apply numbered refactoring patterns with before/after examples. Use when the user invokes /refactor or explicitly asks to refactor, clean up, or improve Python code quality.

SKILL.md

13.0 KB, as published. Nobody here has run it

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

Gives 0 of the 12 instructions most refactoring skills give

Counted across 521 of the 525 authors here whose files we hold, read 2026-08-06

  • run tests after each changein 59 of 521, across 56 files
  • write tests before refactoringin 27 of 521, across 24 files
  • preserve external behaviorin 26 of 521, across 22 files
  • remove dead codein 25 of 521, across 24 files
  • make small incremental changesin 20 of 521, across 17 files
  • break the implementation into tiny commitsin 18 of 521, across 5 files
  • ask the user about alternative optionsin 17 of 521, across 4 files
  • create a GitHub issue with the planin 17 of 521, across 4 files
  • explore the repository to verify assertionsin 17 of 521, across 4 files
  • interview the user about the refactorin 16 of 521, across 3 files
  • check the codebase for test coveragein 16 of 521, across 3 files
  • refactor one thing at a timein 16 of 521, across 12 files

Said here and by no other author read

  • identify smells and map them to patterns
  • run the smell detector first
  • run automated scan for current phase
  • review candidate files for manual patterns
  • provide confidence score for findings
  • run test suite when gate is tests_pass

Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once.

Keep looking

Skills are one crate of 328,083. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.