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:
Discriminated unions with Literal:
NewType for domain primitives:
Protocol for structural typing:
Python-Specific Error Handling
Chain exceptions with from err to preserve the original traceback:
Structured Logging
Use a module-level logger with %s formatting (deferred string interpolation):
Optional: ty
For fast type checking, consider ty from Astral (creators of ruff and uv). Written in Rust, significantly faster than mypy or pyright.
When to choose:
ty— fastest, good for CI and large codebases (early stage, rapidly evolving)pyright— most complete type inference, VS Code integrationmypy— mature, extensive plugin ecosystem


