wshobson avatar

Python Type Safety

A comprehensive guide to Python's type system that goes beyond basic annotations. Covers generics, protocols, and type narrowing with practical patterns you'll actually use. The generic repository examples and protocol definitions are solid starting points for building type-safe APIs. Works well with mypy strict mode and pyright for catching errors before runtime. The Result class and structural typing patterns are especially useful for larger codebases where type safety becomes critical for maintenance.

install copies · 90 days. Times the install command was copied on this site in the last 90 days, bots filtered.
7
skills.sh installs
13.2K
GitHub stars
42.4K
last updated
Aug 21, 2026

Install

$ npx -y skills add wshobson/agents --skill python-type-safety --agent claude-code

Installs into .claude/skills of the current project.

Files

Python Type Safety

Leverage Python's type system to catch errors at static analysis time. Type annotations serve as enforced documentation that tooling validates automatically.

When to Use This Skill

  • Adding type hints to existing code
  • Creating generic, reusable classes
  • Defining structural interfaces with protocols
  • Configuring mypy or pyright for strict checking
  • Understanding type narrowing and guards
  • Building type-safe APIs and libraries

Core Concepts

1. Type Annotations

Declare expected types for function parameters, return values, and variables.

2. Generics

Write reusable code that preserves type information across different types.

3. Protocols

Define structural interfaces without inheritance (duck typing with type safety).

4. Type Narrowing

Use guards and conditionals to narrow types within code blocks.

Quick Start

def get_user(user_id: str) -> User | None:
    """Return type makes 'might not exist' explicit."""
    ...

# Type checker enforces handling None case
user = get_user("123")
if user is None:
    raise UserNotFoundError("123")
print(user.name)  # Type checker knows user is User here

Sponsors

Details

Repository
github.com/wshobson/agents
Category
Python

More from wshobson/agents

All 174

Recommended

More Python
1wshobson avatarpython-configurationwshobson/agentsPython configuration management via environment variables and typed settings. Use when externalizing config, setting up pydantic-settings, managing secrets, or 12Kskills.sh installs
2wshobson avatartemporal-python-testingwshobson/agentsTest Temporal workflows with pytest, time-skipping, and mocking strategies. Covers unit testing, integration testing, replay testing, and local development setu11.3Kskills.sh installs
3affaan-m avatarpython-testingaffaan-m/eccPython testing strategies using pytest, TDD methodology, fixtures, mocking, parametrization, and coverage requirements.6Kskills.sh installs
4affaan-m avatardjango-tddaffaan-m/eccDjango testing strategies with pytest-django, TDD methodology, factory_boy, mocking, coverage, and testing Django REST Framework APIs.5.8Kskills.sh installs
5jeffallan avatardjango-expertjeffallan/claude-skillsUse when building Django web applications or REST APIs with Django REST Framework. Invoke when working with settings.py, models.py, manage.py, or any Django pro4.5Kskills.sh installs
6pydantic avatarpydantic-ai-harnesspydantic/skillsExtend Pydantic AI agents with batteries-included capabilities from pydantic-ai-harness -- Code Mode (collapse many tool calls into one sandboxed Python executi3Kskills.sh installs