Skip to content

pydantic

Classes:

  • StorableModel

    A Pydantic model that can be stored by Labox.

  • StorableSpec

    An annotation for specifying the storage and serialization of a value.

ConfigDict

Bases: StorableConfigDict, ConfigDict

Configuration for a storage model.

See StorableConfigDict and pydantic.ConfigDict for more details.

Attributes:

class_id instance-attribute

class_id: LiteralString | None

An alias for storable_class_id.

storable_class_id instance-attribute

storable_class_id: LiteralString | None

ID of the storable class, as a 16-character hexadecimal string.

storable_unpacker instance-attribute

storable_unpacker: Unpacker | None

ID of the storable class, as a 16-character hexadecimal string.

unpacker instance-attribute

unpacker: Unpacker | None

An alias for storable_unpacker.

StorableModel

Bases: Storable, BaseModel

A Pydantic model that can be stored by Labox.

Methods:

storable_body_serializer

storable_body_serializer(registry: Registry) -> Serializer

Return a JSON serializer for the "body" of this model.

"Body" refers to the data within the model that does not have an explicit storage.

storable_body_storage

storable_body_storage(registry: Registry) -> Storage

Return the storage for the "body" of this model.

"Body" refers to the data within the model that does not have an explicit storage.

storable_config classmethod

storable_config(
    *, allow_none: bool
) -> StorableConfig | None
storable_config(
    *, allow_none: Literal[False] = ...
) -> StorableConfig
storable_config(
    *, allow_none: bool = False
) -> StorableConfig | None

Get the configuration for this storable class.

StorableModelUnpacker

Bases: Unpacker['StorableModel']

Unpacker for StorableModel objects.

Methods:

repack_object

repack_object(
    cls: type[StorableModel],
    contents: Mapping[str, AnyUnpackedValue],
    registry: Registry,
) -> StorableModel

Load the model from storage content.

unpack_object

unpack_object(
    obj: StorableModel, registry: Registry
) -> Mapping[str, AnyUnpackedValue]

Dump the model to storage content.

StorableSpec

An annotation for specifying the storage and serialization of a value.

Use typing.Annotated to add this to any type annotation.

from typing import Any, Annotated, TypeVar
from labox.extra.pydantic import StorableSpec
from labox.extra.msgpack import MsgPackSerializer

msgpack_serializer = MsgPackSerializer()

T = TypeVar("T")
UseMsgPack = Annotated[T, StorableSpec(serializer=msgpack_serializer)]

Then use it somewhere in a storage model:

from labox.extra.pydantic import StorableModel


class MyModel(StorableModel, class_id="..."):
    my_field: UseMsgPack[Any]

Attributes:

serializer class-attribute instance-attribute

serializer: type[Serializer | StreamSerializer] | None = (
    None
)

The serializer to use for this value.

storage class-attribute instance-attribute

storage: type[Storage] | None = None

The storage to use for this value.