Root models and adapters¶
didactic.api.RootModel ¶
Bases: Model
A Model whose value is a single typed payload.
| ATTRIBUTE | DESCRIPTION |
|---|---|
root |
The wrapped value. Must satisfy the type parameter
TYPE:
|
Examples:
>>> import didactic.api as dx
>>> class StringList(dx.RootModel[tuple[str, ...]]):
... pass
>>> StringList(root=("a", "b")).root
('a', 'b')
didactic.api.TypeAdapter ¶
Validate values of an arbitrary type without defining a class.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
The type to validate against. Must be one didactic understands (str, int, float, bool, bytes, datetime, Decimal, UUID, tuple, frozenset, dict, Optional, Literal, Annotated).
TYPE:
|
Examples:
>>> import didactic.api as dx
>>> adapter = dx.TypeAdapter(int)
>>> adapter.validate(42)
42
>>> adapter.validate("not an int")
Traceback (most recent call last):
...
TypeError: ...
validate ¶
Validate value against the adapter's type.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value to validate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T
|
The validated (and possibly coerced) value. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the value cannot be coerced to the target type. |