Modules

dict_deserializer.annotations

class dict_deserializer.annotations.Discriminator[source]

Bases: abc.ABC

Base class for all discriminator.

check(d: dict)[source]

Returns true or false, depending on whether the discriminator matches the provided class.

Parameters:d – the data
Returns:True when this class is valid, False otherwise.
class dict_deserializer.annotations.FunctionDiscriminator(matcher)[source]

Bases: dict_deserializer.annotations.Discriminator

Discriminates according to a custom function.

check(d: dict)[source]

Returns true or false, depending on whether the discriminator matches the provided class.

Parameters:d – the data
Returns:True when this class is valid, False otherwise.
class dict_deserializer.annotations.KeyValueDiscriminator(key, value, has_value=True)[source]

Bases: dict_deserializer.annotations.Discriminator

Discriminates on key and optionally a value.

check(d: dict)[source]

Returns true or false, depending on whether the discriminator matches the provided class.

Parameters:d – the data
Returns:True when this class is valid, False otherwise.
dict_deserializer.annotations.abstract(cls)[source]

Declares that this class cannot be instanced. Only subclasses that are not declared abstract could be instanced.

This is equivalent to setting the class property _abstract=True.

Parameters:cls – The class that should be abstract
Returns:The same class
dict_deserializer.annotations.discriminate(key=None, value=<object object>, matcher=None)[source]

Class level annotation to specify requirements of the raw datastructure in order to be allowed to deserialize into this class (or its subclasses).

Parameters:
  • key – The key to discriminate against
  • value – (Optionally) the value that the property designated by key should hold
  • matcher – (Optionally) a custom function to discriminate with.
Returns:

A function that should wrap the class to be discriminated.

dict_deserializer.annotations.validated(default=None)[source]

Used to decorate a validator function. Can be used if one would want to constrain the value of a property. Of course, @property may be used as well. Throw a TypeError when validation fails.

Parameters:default – The default (initial) value of this property
Returns:the wrapper function.

dict_deserializer.deserializer

class dict_deserializer.deserializer.Deserializable(**kwargs)[source]

Bases: object

Base class for all automagically deserializing classes.

classmethod get_attrs() → Dict[str, dict_deserializer.deserializer.Rule][source]

Returns a list of all type rules for the given class.

Returns:a dict from property to type rule.
class dict_deserializer.deserializer.DeserializableMeta[source]

Bases: type

Metaclass for all Deserializable

class dict_deserializer.deserializer.Rule(type, default=None)[source]

Bases: object

This class is primarily used as a container to store type information.

error_string()[source]
static to_rule(tpe) → dict_deserializer.deserializer.Rule[source]

Ensures type is a rule. Otherwise, it will be converted into a rule.

Parameters:tpe – The type/rule.
Returns:a Rule
validate(key: str, value)[source]

Returns the original value, a default value, or throws. :param key: The key of this field. :param value: Which value to validate. :return: value, default.

dict_deserializer.deserializer.deserialize(rule: dict_deserializer.deserializer.Rule, data, try_all: bool = True, key: str = '[root]')[source]

Converts the passed in data into a type that is compatible with rule.

Parameters:
  • rule
  • data
  • try_all – Whether to attempt other subtypes when a TypeError has occurred. This is useful when automatically deriving discriminators.
  • key – Used for exceptions and error reporting. Preferrably the full path to the current value.
Returns:

An instance matching Rule.

dict_deserializer.deserializer.get_deserialization_classes(t, d, try_all=True) → List[type][source]

Find all candidates that are a (sub)type of t, matching d.

Parameters:
  • t – The type to match from.
  • d – The dict to match onto.
  • try_all – Whether to support automatic discrimination.
Returns:

an ordered list of candidate classes to deserialize into.