dau_core.models¶
Frozen models that validate at use.
Every dau-core model is frozen, and construction validates it. Two things
get past that. model_copy(update=...) builds a new instance without
running a single validator, so a copy can carry a value construction would
have refused. And a frozen model can still hold a dict, which anyone
holding the model can change in place.
CheckedModel closes both. Its instances re-validate whenever they
are handed back to pydantic (revalidate_instances="always"), so a parent
built from a copied child refuses a bad child, and check() is that
re-validation on demand: a validated copy, or the error. replace() is
the validated form of model_copy. A mapping field annotated
ReadOnly is stored as a FrozenMapping, which compares,
hashes and serializes as the dict it was built from and refuses every
mutation.
The ccflow result and callable models cannot change base; they take
CheckedMixin and the same config entries by hand.
Attributes¶
Classes¶
A read-only mapping a frozen model can hold. |
|
|
|
The base of every dau-core data model: frozen, and re-validated at |
Functions¶
|
Module Contents¶
- class dau_core.models.FrozenMapping(data: collections.abc.Mapping[Any, Any] | collections.abc.Iterable[tuple[Any, Any]] = ())[source]¶
Bases:
collections.abc.Mapping[Any,Any]A read-only mapping a frozen model can hold.
A
Mappingrather than adictsubclass: a subclass only overrides the mutating methods, anddict.__setitem__(m, k, v)reaches past any override, so the design that LOOKS safer is the one with a hole in it. Nothing here can write, there is no inherited method that can, and the mapping hashes by content so a frozen model holding one stays hashable.It reads as the dict it was built from – equality, membership, iteration order,
dict(...)– and theReadOnlyannotation carries the serializer that puts a plain dict back on the wire, so the canonical composition JSON (the design-cache key) is unchanged.- __slots__ = ('_data',)¶
- __or__(other: collections.abc.Mapping[Any, Any]) FrozenMapping[source]¶
A NEW mapping with
othermerged in; this one is unchanged.
- dau_core.models.freeze_mapping(value: collections.abc.Mapping[Any, Any]) FrozenMapping[source]¶
- dau_core.models.ReadOnly¶
- class dau_core.models.CheckedMixin[source]¶
checkandreplacefor a frozen pydantic model.- check() Self[source]¶
This model, re-validated: every field constraint and validator runs again, which is what
model_copyskipped.Validation goes through the field VALUES rather than through the instance, because an instance is only revalidated when its own config says so, and a polymorphic base cannot say so (see
CheckedModel). Values, not amodel_dump: a nested model stays a model, so nothing round-trips through JSON to be checked.
- class dau_core.models.CheckedModel(/, **data: Any)[source]¶
Bases:
CheckedMixin,ccflow.BaseModelThe base of every dau-core data model: frozen, and re-validated at every boundary it crosses.
revalidate_instancesis what makes a copied model refuse at the next boundary: a parent built from amodel_copy-mutated child validates the child again and rejects it. It is deliberately NOT on a polymorphic base such asdau_core.generators.CoreGenerator, because a field annotated with the base would revalidate a subclass instance against the base and reject the subclass’s own fields as extra. Those models getCheckedMixinand are checked explicitly.Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_config¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].