marsh.schema.namespace

This module creates support for inheritance-based type matching.

For all types in a namespace each subclass of a type being that is being unmarshaled is considered.

class Namespace(name, base)[source]

Holds a group of subclasses for a base class which are each associated with a name.

This class should be initialized via Namespaces.new().

Includes functionality for matching a type against the classes held by this namespace. This functionality is cached for improved performance.

Parameters:

base (Type[TypeVar(_T)]) – The base class for the namespace.

class CacheInfo(*args, **kwargs)[source]
cache_clear()[source]

Clear the cache.

Return type:

None

cache_info()[source]

Get info for the cache.

Return type:

CacheInfo

Returns:

The cache info.

find_class(cls)[source]

Find the name of a class maybe held by this namespace.

Parameters:

cls (Type[TypeVar(_T)]) – The type to find a name for.

Return type:

Optional[str]

Returns:

None if no class was matched, else the name associated with the type

find_subclasses_iter(cls)[source]

Yield names of components in this namespace that are subclasses of the input type.

This method provides results for the same but cached method find_subclasses().

Parameters:

cls (Type[TypeVar(_T)]) – The input type.

Return type:

Iterator[str]

Returns:

Iterator of string names.

find_subclasses(cls)[source]

Get the names of all classes held by this namespace that are subclasses of the input.

Parameters:

cls (Type[TypeVar(_T)]) – The type to find subclasses for.

Return type:

IterableFromIterator[str]

Returns:

The names of the subclasses.

register(component: Type[_T], *, name: str, replace: bool = False) Type[_T][source]
register(*, name: str, replace: bool = False) Callable[[Type[_T]], Type[_T]]

Register a new type for this namespace.

Parameters:
  • name (str) – The name to associate with the type.

  • replace (bool) – Allow replacing any existing value with the same name.

Return type:

Union[Callable[[Type[TypeVar(_T)]], Type[TypeVar(_T)]], Type[TypeVar(_T)]]

Returns:

A decorator for registering the type if no type was given else the type itself.

build(element: Union[None, int, float, bool, str, Sequence[Any], Mapping[str, Any]] = marsh.MISSING, *, name: str) _T[source]
build(element: Optional[Sequence[ElementType]] = marsh.MISSING, *args, name: str) _T
build(element: Optional[Mapping[str, ElementType]] = marsh.MISSING, *, name: str, **kwargs) _T

Unmarshal a type held by this namespace.

Return type:

TypeVar(_T)

class Namespaces[source]

Singleton class holding all namespaces.

Includes functionality for matching a type against the classes of all namespaces. These matching functions are cached for improved performance.

class CacheInfo(*args, **kwargs)[source]
class FullCacheInfo(*args, **kwargs)[source]
find_subclasses_iter(cls)[source]

Yield results from finding subclasses of an input type in all namespaces.

This method provides results for the same but cached method find_subclasses().

Parameters:

cls (Any) – The input type.

Return type:

Iterator[Tuple[str, Namespace]]

Returns:

Iterator of result.

find_namespaces_iter(cls)[source]

Yield results from finding namespaces matching an input type.

This method provides results for the same but cached method find_namespaces().

Parameters:

cls (Any) – The input type.

Return type:

Iterator[Namespace]

Returns:

Iterator of result.

new(name, base)[source]

Create a new namespace.

The created namespace has functions for registering new items and building them.

Parameters:
  • name (str) – Name of namespace.

  • base (Type[TypeVar(_T)]) – Base class for namespace.

Return type:

Namespace[TypeVar(_T)]

Returns:

The namespace.

cache_clear(full=False)[source]

Clear the cache of this class.

Parameters:

full (bool) – If True, also clear the cache of all namespaces.

Return type:

None

cache_info(full: Literal[True]) Namespaces.FullCacheInfo[source]
cache_info() Namespaces.CacheInfo

Get info for the cache of this class.

Parameters:

full (bool) – If True, return the cache info for all namespaces as well.

Return type:

Union[CacheInfo, FullCacheInfo]

Returns:

The cache info.

find_class(cls)[source]

Search through all namespaces and return the name of the first class that equals the given type.

Parameters:

cls (Any) – The type to find a name for.

Return type:

Optional[str]

Returns:

A name if found, else None.

find_subclasses(cls)[source]

Search for all subclasses of the given type.

Argument:

cls: The type to find subclasses for.

Return type:

IterableFromIterator[Tuple[str, Namespace]]

Returns:

Iterable of names and namespaces.

Parameters:

cls (Any) –

find_namespaces(cls)[source]

Find the namespaces with base classes that are superclasses of the given type.

Parameters:

cls (Any) – The type to find namespaces for.

Return type:

IterableFromIterator[Namespace]

Returns:

Iterable of namespaces found.