AutocastModel: Advanced Coercion for Pydantic Models#
- class my.typing.AutocastModel.AutocastModel#
Pydantic model that automatically casts field values during validation and serialization.
When constructing an instance of a subclass, field values are intelligently cast to match their declared types using
Typist.flexcast(). This eliminates boilerplate validators and makes models more permissive with input formats.During serialization, the model automatically simplifies nested structures in preparation for them to be saved to a flat mapping of some kind, be it YAML, JSON, SQL, or otherwise: enums and times serialize to readable strings, series become plain lists, and nested models become dicts (see
Typist.serialize()).Examples
Declare fields normally; construction coerces, serialization simplifies:
>>> from datetime import datetime >>> from my import AutocastModel >>> class Job(AutocastModel): ... name: str ... priority: int ... tags: list[str] = [] ... due: datetime | None = None >>> job = Job(name=5, priority='3', tags='urgent', due='2026-02-01') >>> job.priority, job.tags (3, ['urgent']) >>> job.model_dump()['due'] '2026-02-01T00:00:00'
- model_config = {}#
Configuration for the model, should be a dictionary conforming to [
ConfigDict][pydantic.config.ConfigDict].