marshals
¶
Support for marshalling Python data structures into primitive equivalents.
Notes
"Marshalling" your data structure prepares it to be serialized into binary and sent over the wire, but does not serialize it. We keep these stages isolated to ensure maximum flexibility and simplicity.
We ensure that your marshalled data is compatible with Python's built-in
json module. This provides maximum compatibility with most serialization
protocols by limiting the output to simple Python builtin types:
Tip
You may use this package directly, but we encourage you to work with our
high-level API provided by the top-level typelib module.
Typical Usage
>>> import dataclasses
>>> import decimal
>>> from typelib import marshals
>>>
>>> @dataclasses.dataclass(slots=True, weakref_slot=True, kw_only=True)
... class Struct:
... key: str
... number: decimal.Decimal
...
>>>
>>> data = Struct(key="some-key", number=decimal.Decimal("1.0"))
>>> marshals.marshal(data)
{'key': 'some-key', 'number': '1.0'}
>>> marshaller = marshals.marshaller(Struct)
>>> marshaller(data)
{'key': 'some-key', 'number': '1.0'}
See Also
Modules:
-
api–The API for marshalling higher-order Python objects into simple, encodable Python objects.
-
routines–Type-specific logic for marshalling Python objects in to simple types for representation over-the-wire.
Classes:
-
AbstractMarshaller–Abstract base class defining the common interface for marshallers.
-
DelayedMarshaller–Delayed proxy for a given type's marshaller, used when we encounter a
typing.ForwardRef. -
EnumMarshaller–A marshaller that converts an
enum.Enuminstance to its assigned value. -
FixedTupleMarshaller–A marshaller for dumping a "fixed" tuple to a simple
list. -
IterableMarshaller–A marshaller for dumping any iterable into a simple
list. -
LiteralMarshaller–A marshaller that enforces the given value be one of the values in the defined
typing.Literal -
MappingMarshaller–A marshaller for dumping any mapping into a simple
dict. -
PatternMarshaller–A marshaller that converts a
re.Patternto a string. -
StructuredTypeMarshaller–A marshaller for dumping a structured (user-defined) type to a simple
dict. -
SubscriptedIterableMarshaller–A marshaller for dumping a subscripted iterable into a simple
list. -
SubscriptedMappingMarshaller–A marshaller for dumping a subscripted mapping into a simple
dict. -
UnionMarshaller–A marshaller for dumping a given value via one of the types in the defined bound union.
Functions:
-
marshal–Marshal
valuefromtypintoMarshalledValueT. -
marshaller–Get a marshaller routine for a given type.
AbstractMarshaller
¶
Abstract base class defining the common interface for marshallers.
Marshallers are custom callables which maintain type-specific information. They use this information to provide robust, performant logic reducing Python objects into their primitive representations for over-the-wire encoding.
Marshallers support contextual serialization, which enables the marshalling of nested types.
Attributes:
-
t(type[T]) –The root type of this marshaller.
-
origin(type[T]) –If
tis a generic, this will be an actionable runtime type related tot, otherwise it is the same ast. -
context(ContextT) –The complete type context for this unmarshaller.
-
var(str | None) –If this unmarshaller is used in a nested context, this will reference the field/parameter/index at which this unmarshaller should be used.
Parameters:
-
(t¶type[T]) –The root type of this marshaller.
-
(context¶ContextT) –The complete type context for this marshaller.
-
(var¶str | None, default:None) –The associated field or parameter name for this unmarshaller (optional).
Source code in src/typelib/marshals/routines.py
DelayedMarshaller
¶
Bases: AbstractMarshaller[T]
Delayed proxy for a given type's marshaller, used when we encounter a typing.ForwardRef.
Notes
This allows us to delay the resolution of the given type reference until call-time, enabling support for cyclic and recursive types.
Attributes:
-
resolved(AbstractMarshaller[T]) –The resolved marshaller.
Source code in src/typelib/marshals/api.py
EnumMarshaller
¶
Bases: AbstractMarshaller[EnumT], Generic[EnumT]
A marshaller that converts an enum.Enum instance to its assigned value.
Parameters:
-
(t¶type[T]) –The root type of this marshaller.
-
(context¶ContextT) –The complete type context for this marshaller.
-
(var¶str | None, default:None) –The associated field or parameter name for this unmarshaller (optional).
Methods:
Source code in src/typelib/marshals/routines.py
__call__
¶
__call__(val: EnumT) -> MarshalledValueT
FixedTupleMarshaller
¶
Bases: AbstractMarshaller[TupleT]
A marshaller for dumping a "fixed" tuple to a simple list.
Values are marshalled according to the value-type in the order they are defined.
See Also
Parameters:
-
(t¶type[TupleT]) –The type to unmarshal from.
-
(context¶ContextT) –Any nested type context. Used to resolve the member marshallers.
-
(var¶str | None, default:None) –A variable name for the indicated type annotation (unused, optional).
Methods:
Source code in src/typelib/marshals/routines.py
IterableMarshaller
¶
Bases: AbstractMarshaller[IterableT], Generic[IterableT]
A marshaller for dumping any iterable into a simple list.
Parameters:
-
(t¶type[T]) –The root type of this marshaller.
-
(context¶ContextT) –The complete type context for this marshaller.
-
(var¶str | None, default:None) –The associated field or parameter name for this unmarshaller (optional).
Methods:
Source code in src/typelib/marshals/routines.py
LiteralMarshaller
¶
Bases: AbstractMarshaller[LiteralT], Generic[LiteralT]
A marshaller that enforces the given value be one of the values in the defined typing.Literal
Parameters:
-
(t¶type[LiteralT]) –The Literal type to enforce.
-
(context¶ContextT) –Nested type context (unused).
-
(var¶str | None, default:None) –A variable name for the indicated type annotation (unused, optional).
Methods:
-
__call__–Enforce the given value is a member of the bound
Literaltype.
Source code in src/typelib/marshals/routines.py
__call__
¶
__call__(val: LiteralT) -> MarshalledValueT
Enforce the given value is a member of the bound Literal type.
Parameters:
-
(val¶LiteralT) –The value to enforce.
Raises:
-
ValueError–If
valis not a member of the boundLiteraltype.
Source code in src/typelib/marshals/routines.py
MappingMarshaller
¶
Bases: AbstractMarshaller[MappingT], Generic[MappingT]
A marshaller for dumping any mapping into a simple dict.
Parameters:
-
(t¶type[T]) –The root type of this marshaller.
-
(context¶ContextT) –The complete type context for this marshaller.
-
(var¶str | None, default:None) –The associated field or parameter name for this unmarshaller (optional).
Methods:
Source code in src/typelib/marshals/routines.py
PatternMarshaller
¶
Bases: AbstractMarshaller[PatternT]
A marshaller that converts a re.Pattern to a string.
Parameters:
-
(t¶type[T]) –The root type of this marshaller.
-
(context¶ContextT) –The complete type context for this marshaller.
-
(var¶str | None, default:None) –The associated field or parameter name for this unmarshaller (optional).
Methods:
-
__call__–Marshal a compiled regex pattern into a string.
Source code in src/typelib/marshals/routines.py
StructuredTypeMarshaller
¶
Bases: AbstractMarshaller[_ST]
A marshaller for dumping a structured (user-defined) type to a simple dict.
See Also
Parameters:
-
(t¶type[_ST]) –The type to unmarshals from.
-
(context¶ContextT) –Any nested type context. Used to resolve the member marshallers.
-
(var¶str | None, default:None) –A variable name for the indicated type annotation (unused, optional).
Methods:
Source code in src/typelib/marshals/routines.py
SubscriptedIterableMarshaller
¶
Bases: AbstractMarshaller[IterableT], Generic[IterableT]
A marshaller for dumping a subscripted iterable into a simple list.
Values are marshalled according to the defined value-type.
See Also
Parameters:
-
(t¶type[IterableT]) –The type to unmarshals from.
-
(context¶ContextT) –Any nested type context. Used to resolve the member marshallers.
-
(var¶str | None, default:None) –A variable name for the indicated type annotation (unused, optional).
Methods:
Source code in src/typelib/marshals/routines.py
SubscriptedMappingMarshaller
¶
Bases: AbstractMarshaller[MappingT], Generic[MappingT]
A marshaller for dumping a subscripted mapping into a simple dict.
Keys are marshalled according to the defined key-type, values according to the defined value-type.
See Also
Parameters:
-
(t¶type[MappingT]) –The type to unmarshals from.
-
(context¶ContextT) –Any nested type context. Used to resolve the member marshallers.
-
(var¶str | None, default:None) –A variable name for the indicated type annotation (unused, optional).
Source code in src/typelib/marshals/routines.py
UnionMarshaller
¶
Bases: AbstractMarshaller[UnionT], Generic[UnionT]
A marshaller for dumping a given value via one of the types in the defined bound union.
See Also
Parameters:
-
(t¶type[UnionT]) –The type to unmarshal into.
-
(context¶ContextT) –Any nested type context. Used to resolve the member marshallers.
-
(var¶str | None, default:None) –A variable name for the indicated type annotation (unused, optional).
Methods:
-
__call__–Marshal a value into the bound
UnionT.
Source code in src/typelib/marshals/routines.py
__call__
¶
__call__(val: UnionT) -> MarshalledValueT
Marshal a value into the bound UnionT.
Parameters:
-
(val¶UnionT) –The input value to unmarshal.
Raises:
-
ValueError–If
valcannot be marshalled via any member type.
Source code in src/typelib/marshals/routines.py
marshal
¶
Marshal value from typ into MarshalledValueT.
Parameters:
-
(value¶Any) –The value to reduce to a simple, encode-able type.
-
(t¶type[T] | ForwardRef | str | None, default:None) –The type to use for building the marshaller (optional). If not provided, we'll default to the type of the input value.
Source code in src/typelib/marshals/api.py
marshaller
¶
marshaller(t: type[T] | ForwardRef | TypeAliasType | str) -> AbstractMarshaller[T]
Get a marshaller routine for a given type.
Parameters:
-
(t¶type[T] | ForwardRef | TypeAliasType | str) –The type annotation to generate a marshaller for. Can be a type, type alias,
typing.ForwardRef, or string reference.