Passed
Pull Request — master (#39)
by Ramon
54s
created

default_mapping_deserializer()   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
cc 3
nop 3
1
from collections import Mapping
2
from typing import Mapping as MappingType
3
from jsons.deserializers import default_dict_deserializer
4
5
6
def default_mapping_deserializer(obj: Mapping, cls: type, **kwargs) -> Mapping:
7
    """
8
    Deserialize a (JSON) list into a set by deserializing all items of that
9
    list. If the list as a generic type (e.g. Set[datetime]) then it is
10
    assumed that all elements can be deserialized to that type.
11
    :param obj: the list that needs deserializing.
12
    :param cls: the type, optionally with a generic (e.g. Set[str]).
13
    :param kwargs: any keyword arguments.
14
    :return: a deserialized set instance.
15
    """
16
    cls_ = Mapping
17
    if hasattr(cls, '__args__'):
18
        cls_ = MappingType[cls.__args__]
19
    dict_ = default_dict_deserializer(obj, cls_, **kwargs)
20
    result = dict_
21
    # Strip any generics from cls to allow for an instance check.
22
    stripped_cls = getattr(cls, '__extra__', cls)
23
    if not isinstance(result, stripped_cls):
24
        result = cls(dict_)
25
    return result
26