Models
pydantic_modelable.model.DefaultDiscriminatorPolicy
Bases: Enum
Describes the method used to determine a Discriminator's default value.
NONE: No default will be setup in the discriminated union FIRST_REGISTERED: The first Modelable extension registered will be used as the default LAST_REGISTERED: The last Modelable extension registered will be used as the default PREDETERMINED: The base Modelable type provides a hard-coded default value on initialization.
Usage examples:
# To use the first extension to MyExtensible ever loaded, provide the enum value:
class MyExtensible(
Modelable,
discriminator='attr',
discriminator_default_policy=DefaultDiscriminatorPolicy.FIRST_REGISTERED,
):
...
# To use the last extension to MyExtensible loaded before instanciation, provide the enum value:
class MyExtensible(
Modelable,
discriminator='attr',
discriminator_default_policy=DefaultDiscriminatorPolicy.LAST_REGISTERED,
):
...
# To use a pre-determined value, as a hard-coded default, provide a string:
class MyExtensible(Modelable, discriminator='attr', discriminator_default_policy='default'):
...
NONE = 0
class-attribute
instance-attribute
FIRST_REGISTERED = 1
class-attribute
instance-attribute
LAST_REGISTERED = 2
class-attribute
instance-attribute
PREDETERMINED = 3
class-attribute
instance-attribute
pydantic_modelable.model.Modelable
Bases: BaseModel
Inherit from this class to define extensible pydantic_modelable models.
Comes with all utilities to register other models as related models to be updated with each loaded extensions of the class derivating this one.
extends_enum(decorable)
classmethod
Decorate String Enum classes based on aenum.Enum.
Register the decorated class as an Enum to be extended with the
discriminating literal from all the child Classes of the one
inheriting Modelable.
Only works in conjunction with inheriting Modelable with the
discriminator keyword argument defined, hinting at which Literal
field should be use as the discrimnator value.
extends_union(attr_name)
classmethod
Decorate pydantic.BaseModel classes.
The decorated class, must be a pydantic.BaseModel. It should contain
a field, that the decorator rewrites as a discriminated Union. The
discriminated union thus configured is made up of all the subclasses of
the class inheriting Modelable.
The parameter attr_name tells the decorator what attribute from the
decorated model should be overwritten as a Discriminated Union field,
and its initial type annotation will not be accounted for.
as_attribute(attr_name, optional=False, default_factory=None)
classmethod
Register a custom pydantic.BaseModel-based class an an attribute.
The attribute to be registered is controlled by the decorator's parameters:
name: name of the attribute to be registered in the decorating classdefault_factory: Optional callable which takes no parameters, and returns a valid instance of the decorated model. Used to provide valid defaults when necessary/useful.
rebuilds_model()
classmethod
Register a model to rebuild, should the Modelable's schema be updated.
This effectively allows a Modelable subclass to trigger rebuilds of the models that include it.
Caveat: If the Modelable object is deep within a hierarchy of models, all parent models may require to be decorated to ensure the full core schema of the model hierarchy is updated.