python-type-safety
wshobson/agents
Add robust Python type hints, generics, and protocols with strict mypy/pyright checking guidance.
What is python-type-safety?
This skill provides guidance and code patterns for adding Python type hints, generics, protocols, and type narrowing, plus configuring mypy/pyright for strict static type checking. Use it when annotating existing code, building type-safe generic classes/APIs, or defining structural interfaces with protocols.
- Provides patterns for annotating function, method, and class signatures
- Shows modern union syntax (T | None) vs older Optional/Union style
- Demonstrates type narrowing with guards for safer handling of optional/union types
- Covers generic class design using TypeVar and Generic, e.g. a Result[T, E] type
- Explains protocols for structural typing without inheritance
- Summarizes best practices for strict static type checking with mypy/pyright
How to install python-type-safety
npx skills add https://github.com/wshobson/agents --skill python-type-safety- Python 3.9+ (3.10+ recommended for modern union syntax)
- mypy or pyright installed for static type checking
How to use python-type-safety
- 1.Identify Python code that needs type annotations, generics, protocols, or stricter type checking
- 2.Apply the relevant pattern: annotate public function/method/class signatures, use T | None union syntax, add type guards for narrowing, or define Generic classes with TypeVar
- 3.Define Protocol classes for structural interfaces where duck typing with type safety is needed
- 4.Configure mypy --strict or pyright (incrementally via per-module overrides for existing projects) and run it in CI
- 5.Consult references/details.md for advanced patterns not covered in the main quick-start examples
Use cases
- Adding type annotations to an existing untyped Python codebase
- Building a generic, reusable container or Result type that preserves type information
- Defining a Protocol-based structural interface instead of using inheritance
- Setting up mypy --strict or pyright in a CI pipeline
- Refactoring code to use type narrowing/guards for safer None handling
- Python developers adding type hints to legacy or untyped code
- Library authors building type-safe, reusable generic APIs
- Teams configuring mypy or pyright strict mode in CI
- Engineers wanting clearer structural interfaces via protocols instead of inheritance
python-type-safety FAQ
Examples target Python 3.10+ for modern union syntax (T | None), but also note the older Optional/Union style needed for Python 3.9.
The SKILL.md describes patterns and recommends running mypy --strict or pyright in CI, but doesn't show actual config file contents in the main content (advanced details may be in references/details.md).
It's focused on static type checking (type hints, generics, protocols, narrowing) validated by tools like mypy/pyright, not runtime validation libraries.
Advanced patterns beyond the core ones (annotations, generics, protocols, narrowing) are referenced in references/details.md, which the agent reads when needed.
Full instructions (SKILL.md)
Source of truth, from wshobson/agents.
name: python-type-safety description: Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright.
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
Fundamental Patterns
Pattern 1: Annotate All Public Signatures
Every public function, method, and class should have type annotations.
def get_user(user_id: str) -> User:
"""Retrieve user by ID."""
...
def process_batch(
items: list[Item],
max_workers: int = 4,
) -> BatchResult[ProcessedItem]:
"""Process items concurrently."""
...
class UserRepository:
def __init__(self, db: Database) -> None:
self._db = db
async def find_by_id(self, user_id: str) -> User | None:
"""Return User if found, None otherwise."""
...
async def find_by_email(self, email: str) -> User | None:
...
async def save(self, user: User) -> User:
"""Save and return user with generated ID."""
...
Use mypy --strict or pyright in CI to catch type errors early. For existing projects, enable strict mode incrementally using per-module overrides.
Pattern 2: Use Modern Union Syntax
Python 3.10+ provides cleaner union syntax.
# Preferred (3.10+)
def find_user(user_id: str) -> User | None:
...
def parse_value(v: str) -> int | float | str:
...
# Older style (still valid, needed for 3.9)
from typing import Optional, Union
def find_user(user_id: str) -> Optional[User]:
...
Pattern 3: Type Narrowing with Guards
Use conditionals to narrow types for the type checker.
def process_user(user_id: str) -> UserData:
user = find_user(user_id)
if user is None:
raise UserNotFoundError(f"User {user_id} not found")
# Type checker knows user is User here, not User | None
return UserData(
name=user.name,
email=user.email,
)
def process_items(items: list[Item | None]) -> list[ProcessedItem]:
# Filter and narrow types
valid_items = [item for item in items if item is not None]
# valid_items is now list[Item]
return [process(item) for item in valid_items]
Pattern 4: Generic Classes
Create type-safe reusable containers.
from typing import TypeVar, Generic
T = TypeVar("T")
E = TypeVar("E", bound=Exception)
class Result(Generic[T, E]):
"""Represents either a success value or an error."""
def __init__(
self,
value: T | None = None,
error: E | None = None,
) -> None:
if (value is None) == (error is None):
raise ValueError("Exactly one of value or error must be set")
self._value = value
self._error = error
@property
def is_success(self) -> bool:
return self._error is None
@property
def is_failure(self) -> bool:
return self._error is not None
def unwrap(self) -> T:
"""Get value or raise the error."""
if self._error is not None:
raise self._error
return self._value # type: ignore[return-value]
def unwrap_or(self, default: T) -> T:
"""Get value or return default."""
if self._error is not None:
return default
return self._value # type: ignore[return-value]
# Usage preserves types
def parse_config(path: str) -> Result[Config, ConfigError]:
try:
return Result(value=Config.from_file(path))
except ConfigError as e:
return Result(error=e)
result = parse_config("config.yaml")
if result.is_success:
config = result.unwrap() # Type: Config
Detailed worked examples and patterns
Detailed sections (starting with ## Advanced Patterns) live in references/details.md. Read that file when the navigation summary above is insufficient.
Best Practices Summary
- Annotate all public APIs - Functions, methods, class attributes
- Use
T | None- Modern union syntax overOptional[T] - Run strict type checking -
mypy --strictin CI - Use generics - Preserve type info in reusable code
- Define protocols - Structural typing for interfaces
- Narrow types - Use guards to help the type checker
- Bound type vars - Restrict generics to meaningful types
- Create type aliases - Meaningful names for complex types
- Minimize
Any- Use specific types or generics.Anyis acceptable for truly dynamic data or when interfacing with untyped third-party code - Document with types - Types are enforceable documentation
Related skills
More from wshobson/agents and the wider catalog.
tailwind-design-system
Build production-ready design systems with Tailwind CSS v4, design tokens, and component libraries.
typescript-advanced-types
Master TypeScript's advanced type system: generics, conditional types, mapped types, and utility types for type-safe applications.
nodejs-backend-patterns
Build production-ready Node.js backends with Express/Fastify, middleware patterns, auth, and database integration.
python-performance-optimization
Profile and optimize Python code using cProfile, memory profilers, and performance best practices.
brand-landingpage
Brand-first landing page designer with guided interviews and Stitch-powered iteration.
python-testing-patterns
Implement comprehensive testing strategies with pytest, fixtures, mocking, and test-driven development.