Skip to content

model.py

PydanticModel dataclass

Bases: IO[BaseModel]

IO to load and save Pydantic models from/to any IO that handles dictionaries.

Example usage:

>>> from pathlib import Path
>>> from ordeq_pydantic import PydanticModel
>>> from pydantic import BaseModel
>>> from ordeq_yaml import YAML

>>> class MyModel(BaseModel):
...     hello: str
...     world: str

>>> dataset = PydanticModel(
...     io=YAML(path=Path("path/to.yaml")),
...     model_type=MyModel
... )

Instead of using:

from pathlib import Path
from ordeq_pydantic import PydanticModel
from ordeq_json import JSON

my_model = PydanticModel(io=JSON(path=Path(...)), model_type=MyModel)

you can also use:

from pathlib import Path
from ordeq_pydantic import PydanticJSON

PydanticJSON(path=Path(...), model_type=MyModel)

This uses the Pydantic JSON implementation which is more efficient for JSON files.

load(**load_options)

Load the Pydantic model from the underlying IO.

Parameters:

Name Type Description Default
**load_options Any

Options to pass to the Pydantic model validation.

{}

Returns:

Type Description
BaseModel

The loaded Pydantic model.

save(model, **save_options)

Save the Pydantic model to the underlying IO.

Parameters:

Name Type Description Default
model BaseModel

The Pydantic model to save.

required
**save_options Any

Options to pass to the Pydantic model dump.

{}