Back to Skills

python-best-practices

Use when reading or writing Python files (.py, pyproject.toml, requirements.txt).

51stars6forksUpdated 7/11/2026

Security Assessment

Safe(100/100)
Security Score100/100

About python-best-practices

Language-specific Python idioms for writing type-first, functional, and safely error-handled code, complementing broader project conventions. Its central theme is making illegal states unrepresentable through the type system. It shows frozen dataclasses for immutable domain models so values cannot be accidentally mutated; discriminated unions built from dataclasses with Literal status fields, consumed with structural pattern matching (match/case) to handle each variant exhaustively; NewType for domain primitives such as UserId and OrderId, so a type checker prevents passing one where another is expected; and Protocol for structural typing, accepting any object that satisfies a shape (for example an object with a read() method) without requiring inheritance. For error handling it demonstrates exception chaining with raise ... from err to preserve the original traceback when wrapping a lower-level error such as a json.JSONDecodeError into a domain-level ValueError. For observability it recommends a module-level logger using %s-style deferred string interpolation rather than eager formatting in log calls. It also covers type-checking tooling, introducing ty from Astral (the makers of ruff and uv) as a fast, Rust-based checker runnable via uvx with no install, and offering guidance on when to choose ty (fastest, good for CI and large codebases, still early stage), pyright (most complete type inference and VS Code integration), or mypy (mature, extensive plugin ecosystem). The material applies when reading or writing Python source, pyproject.toml, or requirements.txt, and gives concrete, copyable snippets for each pattern so teams can adopt consistent, type-safe idioms across a codebase.

FAQ

What does making illegal states unrepresentable mean here?

Use Python's type system — frozen dataclasses, Literal-based discriminated unions, NewType, and Protocol — so invalid states are caught at type-check time rather than at runtime.

How should I wrap a lower-level exception?

Chain it with raise ValueError(...) from err so the original traceback is preserved, as shown when converting a json.JSONDecodeError into a domain-level error.

Why use %s logging instead of f-strings?

The guide recommends a module-level logger with %s formatting for deferred string interpolation, so the message is only formatted when the log level is actually active.

What is ty and when should I use it?

ty is a fast, Rust-based type checker from Astral runnable with uvx ty check; it suits CI and large codebases but is early stage, whereas pyright offers the most complete inference and mypy is the most mature.

How do I keep domain IDs from being mixed up?

Define them with NewType (for example UserId and OrderId) so the type checker prevents passing an OrderId where a UserId is expected.

Install python-best-practices

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill