marsh.schema
¶
This module houses the mechanics that powers marshaling and unmarshaling.
- class MarshalSchema(value, *args, **kwargs)[source]¶
Marshals its value input into a JSON-like object.
- Parameters:
value (
Any
) – The value to match/marshal.
- static doc_static_description()¶
Get a static string description of how the type supported by this schema is used.
- static doc_static_type()¶
Get a static string representation of the type supported by this schema.
- class UnmarshalSchema(*args, default='???', default_factory='???', **kwargs)[source]¶
Unmarshals JSON-like inputs into its value 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:
- class Doc(type=None, default=None, description=None, fields=None, special_fields=None)[source]¶
Structured unmarshal documentation for a type.
- Parameters:
- class Field(doc, type=None, description=None)[source]¶
Unmarshal documentation for a field/attribute of a type.
- class SpecialField(value, description=None)[source]¶
Unmarshal documentation for a special field.
A special field may document any value.
- special_fields: Optional[Sequence[SpecialField]] = None¶
Any other fields that do not follow the name/type/default style.
- has_default()[source]¶
Evaluates if this schema holds a default value.
- Return type:
- Returns:
True
if there is a default value, elseFalse
.
- get_default()[source]¶
Get the default value of this schema.
If default value exists marsh.MISSING is returned instead.
- Return type:
TypeVar
(_T
)- Returns:
The default value or marsh.MISSING.
- select(path)[source]¶
Select a nested schema from this schema based on a dot-separated path. If not all of the path is consumed by this schema it is forwarded to the select method of the nested schema.
- Parameters:
path (
str
) – The path to traverse.- Return type:
- Returns:
The schema at the end of the path.
- doc_type()[source]¶
The name of the type unmarshaled by this schema.
- Return type:
- Returns:
The type name.
- doc_field_type()[source]¶
Alternative name of the type unmarshaled by this schema.
This corresponds to the type that should be display in parent documentation when this is one of its fields.
- Return type:
- Returns:
Alternative type name.
- doc_default()[source]¶
Document default value.
The default implementation of this method returns
None
if no default value exists and'...'
if the default value is mutable. If the default value is immutable, an attempt is first made to marshal the default value before returning a string of its marshaled state. If this fails, a string of the default is returned.
- doc_fields(depth)[source]¶
Documentation for the fields of the type unmarshaled by this schema.
No documentation is returned if no fields exist.
- doc_special_fields()[source]¶
Documentation for the special fields of the type unmarshaled by this schema.
No documentation is returned if no special fields exist.
- Return type:
- Returns:
Special field documentation.
- static doc_static_description()¶
Get a static string description of how the type supported by this schema is used.
- static doc_static_type()¶
Get a static string representation of the type supported by this schema.
- register(schema: Type[_S], /, *, priority: int = 0, lower_priority: Union[None, Type[Schema], int, Iterable[Type[Schema]], Iterable[int]] = None, higher_priority: Union[None, Type[Schema], int, Iterable[Type[Schema]], Iterable[int]] = None, replace: bool = False) Type[_S] [source]¶
- register(*, priority: int = 0, lower_priority: Union[None, Type[Schema], int, Iterable[Type[Schema]], Iterable[int]] = None, higher_priority: Union[None, Type[Schema], int, Iterable[Type[Schema]], Iterable[int]] = None, replace: bool = False) Callable[[Type[_S]], Type[_S]]
Register a new schema (decorator).
Relative priorities can be given as
higher
andlower
, where any schema inhigher
means that the schema being registered should have a lower priority than that, the opposite is true forlower
.The relative priority (if given) takes precedence over the base priority.
- Parameters:
schema (
Optional
[Type
[TypeVar
(_S
, bound=Schema
)]]) – The schema to register.priority (
int
) – Base priority level.lower_priority (
Union
[None
,Type
[Schema
],int
,Iterable
[Type
[Schema
]],Iterable
[int
]]) – Any other schemas that should have lower priority compared to the new schema.higher_priority (
Union
[None
,Type
[Schema
],int
,Iterable
[Type
[Schema
]],Iterable
[int
]]) – Any other schemas that should have higher priority compared to the new schema.replace (
bool
) – Allow replacing existing registered schema with same name.
- Return type:
Union
[Callable
[[Type
[TypeVar
(_S
, bound=Schema
)]],Type
[TypeVar
(_S
, bound=Schema
)]],Type
[TypeVar
(_S
, bound=Schema
)]]- Returns:
Decorator if
schema
was not given, elseschema
.
- caches¶
An instance of
marsh.utils.CachePool
containing the caches used throughoutmarsh.schema