python-best-practices

Model Python data with type-first constructs for enforceable contracts and validation.

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/MileniumTick/skills --skill python-best-practices-mileniumtick
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-best-practices
Source: https://github.com/MileniumTick/skills/tree/main/skills/python-best-practices
Command: npx skills add https://github.com/MileniumTick/skills --skill python-best-practices-mileniumtick

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Python projects often drift away from strong typing, leading to runtime errors and unclear contracts. This skill advocates type-first development using dataclasses, discriminated unions, NewType, Protocols, and TypedDict to establish clear data models and robust interfaces.

Core Features & Use Cases

  • Type-first modeling: Enforce data contracts with dataclasses, TypedDict, and Pydantic models where appropriate.
  • Safe abstractions: Use NewType, Protocols, and Enums to express domain concepts with strong type safety.
  • Guided patterns: Apply practical patterns for error handling, validation, and runtime checks to prevent invalid states.
  • Use Case: Build a domain object for a user with immutable dataclass, strict input types, and a typed API boundary that a linter/type-checker can enforce.

Quick Start

Define a simple User model with a frozen dataclass, a typed API input using TypedDict, and small utility functions with explicit type hints.

Frequently Asked Questions about python-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce type safety in Python to prevent runtime errors?

Enforce type safety in Python by modeling data first with dataclasses, TypedDict, and Protocols. This type-first approach establishes strict interfaces and robust runtime checks, preventing invalid states and unclear contracts across modules.

What is the best way to define strict data contracts using Python type hints?

The best way to define strict data contracts is applying type-first patterns with dataclasses, NewType, and TypedDict. These features express domain concepts clearly and allow static analysis tools to enforce boundaries before runtime.

How do I create safe abstractions for domain concepts in Python?

Create safe abstractions in Python by using NewType, Protocols, and Enums to express domain concepts. This ensures strong type safety and allows static type checkers to verify that strict input types are maintained across modules.

How do I validate API boundaries and handle errors in typed Python projects?

Validate API boundaries in typed Python projects by applying practical patterns for error handling and runtime checks. Define typed API inputs using TypedDict and implement clear error handling to prevent invalid states from propagating.

When should I use discriminated unions and TypedDict over standard classes?

Use discriminated unions and TypedDict when you need to enforce type safety for structured data without full class overhead. They are ideal for establishing clear data models and strict input types at typed API boundaries.

Can I build immutable data models with Python dataclasses and type hints?

Yes, you can build immutable data models using frozen dataclasses with strict input types and explicit type hints. This type-first development pattern establishes robust interfaces that a linter or type-checker can easily enforce.