New advertisers: 50% off your first month with code FIRSTMONTHSee slots
0xbigboss avatar

Python Best Practices

Type-first Python idioms that make illegal states unrepresentable, plus exception chaining, logging, and type checker choices.

skills.sh installs
1.8K
GitHub stars
51
last updated
Jul 14, 2026
View on GitHub

What it does

  • Covers frozen dataclasses, discriminated unions with Literal, NewType domain primitives, and Protocol structural typing
  • Includes exception chaining with from err to preserve tracebacks, and module-level logging with deferred %s formatting
  • Provides guidance on choosing among ty, pyright, and mypy, with uvx ty check usage and [tool.ty] configuration
  • Designed for Python files such as .py, pyproject.toml, and requirements.txt where language-specific idioms apply

Install

$ npx -y skills add 0xbigboss/claude-code --skill python-best-practices -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Python Best Practices

Follows type-first, functional, and error handling patterns from CLAUDE.md. This skill covers language-specific idioms only.

Make Illegal States Unrepresentable

Use Python's type system to prevent invalid states at type-check time.

Frozen dataclasses for immutable domain models:

from dataclasses import dataclass
from datetime import datetime

@dataclass(frozen=True)
class User:
    id: str
    email: str
    name: str
    created_at: datetime

# Frozen dataclasses are immutable — no accidental mutation

Discriminated unions with Literal:

from dataclasses import dataclass
from typing import Literal

@dataclass
class Success:
    status: Literal["success"] = "success"
    data: str

@dataclass
class Failure:
    status: Literal["error"] = "error"
    error: Exception

RequestState = Success | Failure

def handle_state(state: RequestState) -> None:
    match state:
        case Success(data=data):
            render(data)
        case Failure(error=err):
            show_error(err)

NewType for domain primitives:

from typing import NewType

Details

Repository
github.com/0xbigboss/claude-code
Category
Python

More from 0xbigboss/claude-code

All 8

Similar to Python Best Practices

More Python