damast.core.pydantic_export

damast.core.pydantic_export#

Generate pydantic models - dynamically at runtime, or as exported Python source - from a MetaData specification.

Where MetaData describes a dataframe’s columnar schema (vectorized value ranges, aggregate statistics), the models produced here describe a single row/record of that schema. This is useful for validating an individual incoming record (e.g. a single message before it is batched into a dataframe), or for interop with tools that expect a pydantic model (FastAPI, JSON schema export, IDE type-checking).

All entry points are methods of PydanticExporter. PydanticExporter.to_pydantic_model (dynamic), PydanticExporter.generate_pydantic_source (static) and PydanticExporter.to_json_schema (JSON Schema) are all built from the same per-column mapping, so they cannot drift apart from each other.

Note

Only column-level constraints that make sense for a single record are carried over: representation_type (as the field type), is_optional/missing_value (as optionality/default) and value_range (as ge/le for MinMax/CyclicMinMax, or a Literal for ListOfValues). value_stats describes the whole column (e.g. mean, stddev) and has no per-record meaning, so it is not translated into a validation constraint.

A polars.Struct column is supported type-only: its named sub-fields are translated into a nested pydantic.BaseModel recursively, but - since polars’ Struct field only carries a name and a dtype - none of value_range/missing_value/description can be applied to sub-fields, only to the top-level column.

Classes

PydanticExporter()

Generates pydantic models - dynamically at runtime, or as exported Python source - from a MetaData specification.