Supported Types

Marshal

Marshal Types

Type

Description

slice

Marshals a slice into a mapping with the keys "start", "stop" and "step".

int, float, bool, str

Marshals any instance of a primitive class or subclass into its baseclass value.

DictConfig, ListConfig

Marshals an omegaconf DictConfig or ListConfig, converting all enums in the container to strings.

None

Simply marshals any None value by returning it.

namedtuple(), NamedTuple

Marshals namedtuples into a mapping where the keys correspond to the field names of the namedtuple. Supports both namedtuple() and NamedTuple.

Logger

Marshals a python logger into a mapping with keys "name" and "level".

Enum

Marshals an enum instance into its name.

datetime.datetime, datetime.time, datetime.date

Marshals a datetime object into formatted string according to ISO. See datetime.datetime.isoformat(), .:func:datetime.time.isoformat and datetime.date.isoformat().

dataclass()

Marshals a dataclass instance into a mapping with the field names as keys.

complex

Marshals a complex value into a dictionary with keys "real" and "imag" containing float values.

bytes

Marshals bytes into a URL-safe base64-encoded string.

SequenceProtocol

Unmarshals sequence values matched through ducktyping.Uses SequenceProtocol as reference for the ducktyped match. Marshaling outputs a JSON-style list.

MappingProtocol

Unmarshals mapping values matched through ducktyping.Uses MappingProtocol as reference for the ducktyped match. Marshaling outputs a JSON-style map.

Iterable

Fetches and marshals all items in an iterable.

Unmarshal

Unmarshal Types

Type

Description

Annotated

Unwraps and delegates unmarshaling to the underlying type.

Union

The union type which in later versions of python may be specified using the | operator. The order of the types in the union matter as an attempt is made to unmarshal each type where the value of the first successful unmarshal is returned. Optional will always set None as the second type in the produced union.

TypeVar

Unwraps the TypeVar, using its bound or constraints when unmarshaling. If no bound or constraints are present the type can not be unmarshaled.

TypedDict

Unmarshals any subclass of TypedDict from a mapping input. If total=False then all keys are optional.

slice

Accepts input as strings in the python slicing format (i.e. “1:2”, “::-1” e.t.c). Also accepts keys “start”, “stop” and/or “step” or a sequence of up to 3 optional integer values. If a single integer is given, it is treated as “stop”

set

Unmarshals a sequence input into a set. Any duplicate values will be discarded.

Protocol

Attempts to unmarshal runtime_checkable() Protocols into a matching class. The available classes. that can be matched against are float, int, bool, bytes, dict, list, str and complex.

int, float, bool, str

Unmarshals any primitive class or its subclass. Special rules for bool is documented in primitive_to_bool().

None

A missing value is valid input as well as the None value directly.

namedtuple(), NamedTuple

Unmarshals namedtuple types from a mapping or sequence input. Supports both namedtuple() and NamedTuple.

tuple

Unmarshals a sequence input into a fixed size tuple. The input sequence must contain the same number of elements as the tuple type specifies.

Logger

A python logging.Logger. If no arguments are given the root logger is used. Takes either a single string which is used as name for the logger or a mapping with optional keys “name” and “level”. The level can be given as a string or an integer.

Literal

Matches the input against the literal value(s), then returns the matched value.

Enum

Input must match one of the enum values (the name or value).

Sequence

Unmarshals a sequence type from a sequence input. Should work for all sequence types. If the sequence type is one of the abstract sequence classes in collections.abc then a suitable replacement type is implicitly used instead.

defaultdict

Unmarshals an input mapping into a defaultdict. If no default value is given in the type, all values default to None. An attempt is made to initialize the default value with a zero-value (zero, empty sequence, empty mapping, None e.t.c). This is not always guaranteed to work if the default type requires specific arguments to be initialized. Use with caution.

Mapping

Unmarshals a mapping type from a mapping input. Should work for all mapping types. If the mapping type is one of the abstract mapping classes in collections.abc then a suitable replacement type is implicitly used instead.

datetime.datetime, datetime.time, datetime.date

For string inputs the format must be compatible with dateparser.parse(). The date order is D M Y, not M D Y. A float value can also be used as input in which case it is treated as a unix timestamp. A mapping or sequence as input will be passed directly to datetime as keyword or positional arguments.

dataclass()

Expects a mapping input with the dataclass field names as keys.

complex

Expects a mapping with optional keys “real” and/or “imag” with float values or a sequence containing up to two float values, the first being the real value and the second being the imaginary value. If passed a float value instead of a mapping or sequence, it is treated as the real value.

bytes

A valid (optionally URL-safe) base64-encoded string is expected as input.

Any

Any JSON-like data. Values should consist of one or more of the types int, float, str, bool, Sequence or Mapping. Can not contain missing values.

Callable

Covers anything that is callable, be it a class, function or callable class instance. Callable arguments are inspected and the input should be a sequence or mapping(similar to *args or **kwargs).

SequenceProtocol

Unmarshals sequence types matched through ducktyping.Uses SequenceProtocol as reference for the ducktyped match. Expects input to be a JSON-style list during unmarshaling.

MappingProtocol

Unmarshals mapping types matched through ducktyping.Uses MappingProtocol as reference for the ducktyped match. Expects input to be a JSON-style map during unmarshaling.