marsh.schema.core

The core functionality powering marshaling and unmarshaling.

class Schema(value, *args, **kwargs)[source]

Base class for schemas.

A schema is a class that can be matched against a value and then initialized with that value and any other arguments.

The base class works as a factory when its called as a constructor, selecting the correctly matched schema and returning an instance of it.

Parameters:
  • value (Any) – The value to match.

  • args – Unused but caught positional arguments.

  • kwargs – Unused but caught keyword arguments.

classmethod match(value)[source]

Match this class against a value.

Parameters:

value (Any) – The value to match against.

Return type:

bool

Returns:

True if matched, else False.

static doc_static_type()[source]

Get a static string representation of the type supported by this schema.

Return type:

Optional[str]

Returns:

The name of the type.

static doc_static_description()[source]

Get a static string description of how the type supported by this schema is used.

Return type:

Optional[str]

Returns:

The description.

class SchemaMeta[source]

Metaclass for schemas.

If calling the constructor on the base schema class it will instead match the input with all registered schemas and return an instance of a subclass.

registry: SchemaRegistry[_S] = <marsh.schema.core.base.SchemaRegistry object>

The registered schema types.

class SchemaRegistry(error=<class 'marsh.errors.MarshError'>)[source]

Holds schema classes in a prioritized order.

Parameters:

error (Type[Exception]) – Type of the error to throw when no schema could be matched during selection.

match(value)[source]

Match a value to one or more schemas types.

Selected schema types are returned in the order they were match. The matching is canceled and the results are returned when the first non-wrapper schema type has been matched.

Raises an error if no non-wrapper schema could be matched.

Parameters:

value (Any) – Value to match against schema types.

Return type:

SchemaSelection[TypeVar(_S, bound= Schema)]

Returns:

Selection of schema types.

class SchemaSelection(iterable=(), /)[source]

A sequence of schema types that were matched against a value.

Contains one or more schema types, with all but the last one being wrapper classes.

build(*args, **kwargs)[source]

Build the selected schema(s) into a single schema.

Parameters:
  • args – Any positional arguments to pass on to the schema constructor.

  • kwargs – Any keyword arguments to pass on to the schema constructor.

Return type:

TypeVar(_S, bound= Schema)

Returns:

Schema instance.