prefer-semantic-temporal-types
Use semantic types for temporal fields and function boundaries.
Why
Primitive numbers erase the distinction between instants and durations and allow accidental mixing of units. Strings leave Pydantic instant fields unparsed.
Fix
Represent instants with datetime, or Pydantic AwareDatetime when timezone awareness is required. Represent durations and TTLs with timedelta. Convert explicit units at boundaries and preserve the serialized contract.
Examples
from pydantic import BaseModel
class CacheEntry(BaseModel): created_ms: int ttl_seconds: float = 60from datetime import timedeltafrom pydantic import BaseModel, AwareDatetime
class CacheEntry(BaseModel): created_at: AwareDatetime ttl: timedeltaimport pydantic_settings as ps
class Settings(ps.BaseSettings): created_ms: intimport pydantic_settings as psfrom pydantic import AwareDatetime
class Settings(ps.BaseSettings): created_at: AwareDatetimefrom pydantic import BaseModel
class CacheEntry(BaseModel): queued_at: str
def flush(timeout_seconds: float | None) -> None: passfrom datetime import timedeltafrom pydantic import AwareDatetime, BaseModel
class CacheEntry(BaseModel): queued_at: AwareDatetime
def flush(timeout: timedelta | None) -> None: pass