← ClaudeAtlas

python-best-practiceslisted

Provides Python patterns for type-first development with dataclasses, discriminated unions, NewType, and Protocol. Must use when reading or writing Python files.
aiskillstore/marketplace · ★ 329 · Data & Documents · score 79
Install: claude install-skill aiskillstore/marketplace
# Python Best Practices ## Type-First Development Types define the contract before implementation. Follow this workflow: 1. **Define data models** - dataclasses, Pydantic models, or TypedDict first 2. **Define function signatures** - parameter and return type hints 3. **Implement to satisfy types** - let the type checker guide completeness 4. **Validate at boundaries** - runtime checks where data enters the system ### Make Illegal States Unrepresentable Use Python's type system to prevent invalid states at type-check time. **Dataclasses for structured data:** ```python from dataclasses import dataclass from datetime import datetime @dataclass(frozen=True) class User: id: str email: str name: str created_at: datetime @dataclass(frozen=True) class CreateUser: email: str name: str # Frozen dataclasses are immutable - no accidental mutation ``` **Discriminated unions with Literal:** ```python from dataclasses import dataclass from typing import Literal @dataclass class Idle: status: Literal["idle"] = "idle" @dataclass class Loading: status: Literal["loading"] = "loading" @dataclass class Success: status: Literal["success"] = "success" data: str @dataclass class Failure: status: Literal["error"] = "error" error: Exception RequestState = Idle | Loading | Success | Failure def handle_state(state: RequestState) -> None: match state: case Idle(): pass case Loading(): show_spinne