pythonic-code

Write, refactor, and review Python code for clarity, strong typing, and minimal abstraction.

4.0k|282|Updated Dec 2, 2024
One-click install
npx skills add https://github.com/basicmachines-co/basic-memory --skill pythonic-code-basicmachines-co
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: pythonic-code
Source: https://github.com/basicmachines-co/basic-memory/tree/main/.agents/skills/pythonic-code
Command: npx skills add https://github.com/basicmachines-co/basic-memory --skill pythonic-code-basicmachines-co

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve? Python codebases often accumulate unnecessary class hierarchies, helper-heavy indirection, and weak typing that make behavior hard to trace for both humans and AI agents. This Skill provides a disciplined decision framework for writing, refactoring, and reviewing Python so domain behavior stays explicit and locally readable. ## Core Features & Use Cases - Constructive Domain Modeling: Define valid states with frozen dataclasses, closed unions via Python 3.12 type aliases, and Pydantic models at validation boundaries, consumed with exhaustive match statements. - Refactoring Guidance: Simplify object-heavy or procedural code by replacing property-only protocols and single-implementation abstractions with direct module-level functions and typed values. - Code Review Mode: Report concrete readability, typing, lifecycle, and domain-model risks with the smallest practical improvement, without editing unless asked. - Use Case: When refactoring a service layer where a full EntityService is constructed just to prepare note content, use this Skill to restructure the path into direct functions over frozen typed values while preserving observable behavior and test coverage. ## Quick Start Use the pythonic-code skill to refactor this Python module for clarity, explicit typing, and minimal abstraction while preserving its current behavior.

Frequently Asked Questions about pythonic-code

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor Python code with too many classes and helpers?▼

Start with ordinary fully typed functions and frozen dataclasses, extracting helpers only when they capture a domain operation or isolate a side effect. Treat classes dominated by private methods as a signal to move behavior into module-level functions over typed values.

What is constructive domain modeling in Python?▼

Constructive domain modeling defines the valid values a program can construct rather than listing invalid combinations. Represent single states with frozen dataclasses, alternatives with closed unions using Python 3.12 type aliases, and consume them with exhaustive match statements.

When should I use Pydantic versus dataclasses in Python?▼

Use Pydantic models at API, CLI, MCP, configuration, and persistence boundaries where untrusted values need runtime validation or serialization. Use frozen dataclasses for internal domain values, since a Pydantic model is not automatically the best internal state representation.

Does this approach work with Python 3.12 type aliases and match statements?▼

Yes, the guidance targets Python 3.12 and newer, using type aliases for closed unions and match statements for exhaustive consumption. typing.assert_never proves exhaustive handling so newly added variants surface as type errors.

When should exceptions be used instead of result types in Python?▼

Keep exceptions for broken invariants, cancellation, and unpredictable filesystem, network, queue, or database failures. Return explicit union variants for recoverable domain outcomes when callers can respond differently, and avoid wrapping every operation in Result types.