marsh

Root of the marsh framework.

Exposes the basic functionality and modules of the framework.

marshal(value)[source]

Serialize a value to a JSON-like object.

Parameters:

value (Any) – The input value to serialize.

Return type:

Union[None, int, float, bool, str, Sequence[Any], Mapping[str, Any]]

Returns:

The JSON representation of the object.

unmarshal(type_: Type[_T], element: Union[None, int, float, bool, str, Sequence[Any], Mapping[str, Any]] = marsh.MISSING) _T[source]
unmarshal(type_: Type[_T], element: Mapping[str, ElementType] = marsh.MISSING, **kwargs) _T

Unmarshal a JSON-like object to some type.

Note

Static type checkers may give an error for abstract types (issue).

Some type aliases are also not recognized as types. For the moment there is not better solution than using a simple # type: ignore comment.

Parameters:
Return type:

TypeVar(_T)

Returns:

The unmarshaled value.

cast(type_, value='???')[source]

Cast a value to some type. The input value is serialized to a JSON-like object and then used for constructing an instance of the specified type.

Note

Static type checkers may give an error for abstract types (issue).

Some type aliases are also not recognized as types. For the moment there is not better solution than using a simple # type: ignore comment.

Parameters:
  • type – The type to cast to.

  • value (Any) – The value to cast.

  • type_ (Type[_T]) –

Return type:

TypeVar(_T)

Returns:

The casted value.

unmarshal_args(callable_, element='???')[source]

Return the unmarshaled arguments of the given function.

Parameters:
Return type:

Arguments

Returns:

Tuple of positional arguments and keyword arguments.

cast_args(callable_, value='???')[source]

Return the casted arguments of the given function.

The given input will be marshaled before being unmarshaled into the function arguments.

Parameters:
  • callable – The function whos arguments to unmarshal.

  • value (Any) – The input.

  • callable_ (Callable) –

Return type:

Arguments

Returns:

Tuple of positional arguments and keyword arguments.

main(*, setup_logging: bool = False, prog: Optional[str] = None, description: Optional[str] = None, epilog: Optional[str] = None, help_depth: int = 1) Callable[[_C], _C][source]
main(fn: _C, /, *, setup_logging: bool = False, prog: Optional[str] = None, description: Optional[str] = None, epilog: Optional[str] = None, help_depth: int = 1) _C
main(*, config: Type[_T], setup_logging: bool = False, prog: Optional[str] = None, description: Optional[str] = None, epilog: Optional[str] = None, help_depth: int = 1) Callable[[Callable[[_T], _R]], Callable[[_T], _R]]
main(fn: Callable[[_T], _R], /, *, config: Type[_T], setup_logging: bool = False, prog: Optional[str] = None, description: Optional[str] = None, epilog: Optional[str] = None, help_depth: int = 1) Callable[[_T], _R]

Decorates a function, allowing it to be called as an entry point for a python script being run from the command line.

If a config class is supplied, the decorated function will be passed an instance of that class as a single positional argument. If no config class is supplied, the arguments for the decorated function are filled out and passed to the function.

Parameters:
  • fn – The callable function to decorate. When decorating, this argument should not be explicitly specified.

  • config (Optional[Any]) – An optional config class.

  • setup_loggin – Add console line arguments for logging level and format. Initiallizes logging through logging.basicConfig().

  • prog (Optional[str]) – Optional program name. Defaults to the first value of sys.argv.

  • description (Optional[str]) – Optional description shown in the help message. Defaults to the docstring of the decorated function (or the config class if passed to the decorator). Can be set to None to disable completely.

  • epilog (Optional[str]) – Optional epilog shown in the help message.

  • help_depth (int) – How many subfields of a config to flatten for --help [PATH].

Returns:

The wrapped function.

MISSING

Represents the missing value, i.e. when a value is unset (which is not equivalent to a value being None).

Useful for when a default value is required by python even though there should not be one. marsh treats this the same as if there were no default value.

The concrete value of MISSING is the string '???'.

See marsh.MISSING for more info.

namespaces: marsh.schema.namespace.Namespaces

Gives access to all namespaces. Also allows for new namespaces to be created through its new(...) method.

__version__: str

The current version of marsh.