python-best-practices
Use when reading or writing Python files (.py, pyproject.toml, requirements.txt).
By alleneubank · 2,061 installs
npx skills add alleneubank/claude-code --skill python-best-practices
Source repository · Upstream listing
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](https://docs.astral.sh/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 integration
mypy — mature, extensive plugin ecosystem