python-best-practices

Solid

Provides Python patterns for type-first development with dataclasses, discriminated unions, NewType, and Protocol. Must use when reading or writing Python files.

AI & Automation 11 stars 0 forks Updated today MIT

Install

View on GitHub

Quality Score: 79/100

Stars 20%
36
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 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...

Details

Author
NoesisVision
Repository
NoesisVision/nasde-toolkit
Created
4 months ago
Last Updated
today
Language
Python
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category