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.

class Positive[source]

Validate that a number is non-zero and positive.

class Negative[source]

Validate that a number is non-zero and negative.

class Unsigned[source]

Validate that a number is zero or higher.

class Populated[source]

Validate that a collection contains at least one item.