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
python
1def get_user(user_id: str) -> User | None:
2 """Return type makes 'might not exist' explicit."""
3 ...
4
5# Type checker enforces handling None case
6user = get_user("123")
7if user is None:
8 raise UserNotFoundError("123")
9print(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.
python
1def get_user(user_id: str) -> User:
2 """Retrieve user by ID."""
3 ...
4
5def process_batch(
6 items: list[Item],
7 max_workers: int = 4,
8) -> BatchResult[ProcessedItem]:
9 """Process items concurrently."""
10 ...
11
12class UserRepository:
13 def __init__(self, db: Database) -> None:
14 self._db = db
15
16 async def find_by_id(self, user_id: str) -> User | None:
17 """Return User if found, None otherwise."""
18 ...
19
20 async def find_by_email(self, email: str) -> User | None:
21 ...
22
23 async def save(self, user: User) -> User:
24 """Save and return user with generated ID."""
25 ...
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.
python
1# Preferred (3.10+)
2def find_user(user_id: str) -> User | None:
3 ...
4
5def parse_value(v: str) -> int | float | str:
6 ...
7
8# Older style (still valid, needed for 3.9)
9from typing import Optional, Union
10
11def find_user(user_id: str) -> Optional[User]:
12 ...
Pattern 3: Type Narrowing with Guards
Use conditionals to narrow types for the type checker.
python
1def process_user(user_id: str) -> UserData:
2 user = find_user(user_id)
3
4 if user is None:
5 raise UserNotFoundError(f"User {user_id} not found")
6
7 # Type checker knows user is User here, not User | None
8 return UserData(
9 name=user.name,
10 email=user.email,
11 )
12
13def process_items(items: list[Item | None]) -> list[ProcessedItem]:
14 # Filter and narrow types
15 valid_items = [item for item in items if item is not None]
16 # valid_items is now list[Item]
17 return [process(item) for item in valid_items]
Pattern 4: Generic Classes
Create type-safe reusable containers.
python
1from typing import TypeVar, Generic
2
3T = TypeVar("T")
4E = TypeVar("E", bound=Exception)
5
6class Result(Generic[T, E]):
7 """Represents either a success value or an error."""
8
9 def __init__(
10 self,
11 value: T | None = None,
12 error: E | None = None,
13 ) -> None:
14 if (value is None) == (error is None):
15 raise ValueError("Exactly one of value or error must be set")
16 self._value = value
17 self._error = error
18
19 @property
20 def is_success(self) -> bool:
21 return self._error is None
22
23 @property
24 def is_failure(self) -> bool:
25 return self._error is not None
26
27 def unwrap(self) -> T:
28 """Get value or raise the error."""
29 if self._error is not None:
30 raise self._error
31 return self._value # type: ignore[return-value]
32
33 def unwrap_or(self, default: T) -> T:
34 """Get value or return default."""
35 if self._error is not None:
36 return default
37 return self._value # type: ignore[return-value]
38
39# Usage preserves types
40def parse_config(path: str) -> Result[Config, ConfigError]:
41 try:
42 return Result(value=Config.from_file(path))
43 except ConfigError as e:
44 return Result(error=e)
45
46result = parse_config("config.yaml")
47if result.is_success:
48 config = result.unwrap() # Type: Config
Advanced Patterns
Pattern 5: Generic Repository
Create type-safe data access patterns.
python
1from typing import TypeVar, Generic
2from abc import ABC, abstractmethod
3
4T = TypeVar("T")
5ID = TypeVar("ID")
6
7class Repository(ABC, Generic[T, ID]):
8 """Generic repository interface."""
9
10 @abstractmethod
11 async def get(self, id: ID) -> T | None:
12 """Get entity by ID."""
13 ...
14
15 @abstractmethod
16 async def save(self, entity: T) -> T:
17 """Save and return entity."""
18 ...
19
20 @abstractmethod
21 async def delete(self, id: ID) -> bool:
22 """Delete entity, return True if existed."""
23 ...
24
25class UserRepository(Repository[User, str]):
26 """Concrete repository for Users with string IDs."""
27
28 async def get(self, id: str) -> User | None:
29 row = await self._db.fetchrow(
30 "SELECT * FROM users WHERE id = $1", id
31 )
32 return User(**row) if row else None
33
34 async def save(self, entity: User) -> User:
35 ...
36
37 async def delete(self, id: str) -> bool:
38 ...
Pattern 6: TypeVar with Bounds
Restrict generic parameters to specific types.
python
1from typing import TypeVar
2from pydantic import BaseModel
3
4ModelT = TypeVar("ModelT", bound=BaseModel)
5
6def validate_and_create(model_cls: type[ModelT], data: dict) -> ModelT:
7 """Create a validated Pydantic model from dict."""
8 return model_cls.model_validate(data)
9
10# Works with any BaseModel subclass
11class User(BaseModel):
12 name: str
13 email: str
14
15user = validate_and_create(User, {"name": "Alice", "email": "a@b.com"})
16# user is typed as User
17
18# Type error: str is not a BaseModel subclass
19result = validate_and_create(str, {"name": "Alice"}) # Error!
Pattern 7: Protocols for Structural Typing
Define interfaces without requiring inheritance.
python
1from typing import Protocol, runtime_checkable
2
3@runtime_checkable
4class Serializable(Protocol):
5 """Any class that can be serialized to/from dict."""
6
7 def to_dict(self) -> dict:
8 ...
9
10 @classmethod
11 def from_dict(cls, data: dict) -> "Serializable":
12 ...
13
14# User satisfies Serializable without inheriting from it
15class User:
16 def __init__(self, id: str, name: str) -> None:
17 self.id = id
18 self.name = name
19
20 def to_dict(self) -> dict:
21 return {"id": self.id, "name": self.name}
22
23 @classmethod
24 def from_dict(cls, data: dict) -> "User":
25 return cls(id=data["id"], name=data["name"])
26
27def serialize(obj: Serializable) -> str:
28 """Works with any Serializable object."""
29 return json.dumps(obj.to_dict())
30
31# Works - User matches the protocol
32serialize(User("1", "Alice"))
33
34# Runtime checking with @runtime_checkable
35isinstance(User("1", "Alice"), Serializable) # True
Pattern 8: Common Protocol Patterns
Define reusable structural interfaces.
python
1from typing import Protocol
2
3class Closeable(Protocol):
4 """Resource that can be closed."""
5 def close(self) -> None: ...
6
7class AsyncCloseable(Protocol):
8 """Async resource that can be closed."""
9 async def close(self) -> None: ...
10
11class Readable(Protocol):
12 """Object that can be read from."""
13 def read(self, n: int = -1) -> bytes: ...
14
15class HasId(Protocol):
16 """Object with an ID property."""
17 @property
18 def id(self) -> str: ...
19
20class Comparable(Protocol):
21 """Object that supports comparison."""
22 def __lt__(self, other: "Comparable") -> bool: ...
23 def __le__(self, other: "Comparable") -> bool: ...
Pattern 9: Type Aliases
Create meaningful type names.
Note: The type statement was introduced in Python 3.10 for simple aliases. Generic type statements require Python 3.12+.
python
1# Python 3.10+ type statement for simple aliases
2type UserId = str
3type UserDict = dict[str, Any]
4
5# Python 3.12+ type statement with generics
6type Handler[T] = Callable[[Request], T]
7type AsyncHandler[T] = Callable[[Request], Awaitable[T]]
8
9# Python 3.9-3.11 style (needed for broader compatibility)
10from typing import TypeAlias
11from collections.abc import Callable, Awaitable
12
13UserId: TypeAlias = str
14Handler: TypeAlias = Callable[[Request], Response]
15
16# Usage
17def register_handler(path: str, handler: Handler[Response]) -> None:
18 ...
Pattern 10: Callable Types
Type function parameters and callbacks.
python
1from collections.abc import Callable, Awaitable
2
3# Sync callback
4ProgressCallback = Callable[[int, int], None] # (current, total)
5
6# Async callback
7AsyncHandler = Callable[[Request], Awaitable[Response]]
8
9# With named parameters (using Protocol)
10class OnProgress(Protocol):
11 def __call__(
12 self,
13 current: int,
14 total: int,
15 *,
16 message: str = "",
17 ) -> None: ...
18
19def process_items(
20 items: list[Item],
21 on_progress: ProgressCallback | None = None,
22) -> list[Result]:
23 for i, item in enumerate(items):
24 if on_progress:
25 on_progress(i, len(items))
26 ...
Configuration
Strict Mode Checklist
For mypy --strict compliance:
toml
1# pyproject.toml
2[tool.mypy]
3python_version = "3.12"
4strict = true
5warn_return_any = true
6warn_unused_ignores = true
7disallow_untyped_defs = true
8disallow_incomplete_defs = true
9no_implicit_optional = true
Incremental adoption goals:
- All function parameters annotated
- All return types annotated
- Class attributes annotated
- Minimize
Any usage (acceptable for truly dynamic data)
- Generic collections use type parameters (
list[str] not list)
For existing codebases, enable strict mode per-module using # mypy: strict or configure per-module overrides in pyproject.toml.
Best Practices Summary
- Annotate all public APIs - Functions, methods, class attributes
- Use
T | None - Modern union syntax over Optional[T]
- Run strict type checking -
mypy --strict in 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. Any is acceptable for truly dynamic data or when interfacing with untyped third-party code
- Document with types - Types are enforceable documentation