marsh.annotation
¶
Annotations that can be used with typing.Annotated
.
Annotations may be used to customize the unmarshaling behavior through transforms or validations.
from typing import Annotated
import marsh
UnsignedInt = Annotated[int, marsh.annotation.Unsigned]
value = marsh.unmarshal(UnsignedInt, 5) # works
value = marsh.unmarshal(UnsignedInt, -5) # fails
Create your own annotations by inheriting Annotation
.
from typing import Annotated
import marsh
class Abs(marsh.annotation.Annotation):
def __call__(
self,
value
):
return abs(value)
AbsInt = Annotated[int, Abs]
assert marsh.unmarshal(AbsInt, -5) == 5
- class Annotation[source]¶
Base class for annotations recognized by
marsh
.These can be used with
typing.Annotated
to include validation or transformation of the unmarshal result.