name: python-modern-typing description: >- Strict modern Python typing standards (Python 3.10+ / 3.12). Covers built-in generics, union operator, future annotations, and SQLAlchemy Mapped[] type safety.
Modern Python Typing Standards
Rules for writing clean, robust, and PEP-compliant type hints in Python 3.10+.
Rules
1. Future Annotations
- Always put
from __future__ import annotationsat the top of every Python file. - This delays evaluation of type annotations, avoiding import circularity and enabling forward-references.
2. Built-in Generics (PEP 585)
- Never import capitalised generic types from
typing(e.g.,List,Dict,Tuple,Set). - Use built-in lowercase collections instead:
list,dict,tuple,set.
3. Union Operator (PEP 604)
- Never use
typing.Unionortyping.Optional. - Use the
|operator for unions (e.g.,str | int). - Use
T | Noneinstead ofOptional[T].
4. Strict Type Safety
- No
Any: Never useAny. Be explicit. Useobjector generic type variables if necessary. - Annotate Everything: All function signatures must type-hint all arguments and return values.
5. SQLAlchemy ORM Type Safety
- Ensure
Mapped[]wrapper matches the column SQL type precisely (e.g.,Mapped[datetime]instead ofMapped[object]orMapped[str]). - Do not use bare
Mappedwithout a type parameter.
Examples
Good Typing Style
from __future__ import annotations
from collections.abc import Sequence
from uuid import UUID
def process_items(
items: list[str],
prefix: str | None = None
) -> dict[str, list[int]]:
result: dict[str, list[int]] = {}
# implementation...
return result
Bad Typing Style
from typing import List, Dict, Optional
def process_items(
items: List[str],
prefix: Optional[str] = None
) -> Dict[str, List[int]]:
# ...
Anti-Patterns
| ❌ Don't | ✅ Do instead |
|---|---|
from typing import List, Dict, Optional |
Use list, dict, `str |
data: Any |
Use specific models, dict[str, object], or object |
Mapped[object] for datetime columns |
Mapped[datetime] |
Optional[Sequence[int]] |
`Sequence[int] |