Completed
Pull Request — master (#39)
by Ramon
01:14
created

default_mapping_deserializer()   A

Complexity

Conditions 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
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: dict, cls: type, **kwargs) -> Mapping:
7
    """
8
    Deserialize a (JSON) dict into a mapping by deserializing all items of that
9
    dict.
10
    :param obj: the dict that needs deserializing.
11
    :param cls: the type, optionally with a generic (e.g. Set[str]).
12
    :param kwargs: any keyword arguments.
13
    :return: a deserialized set instance.
14
    """
15
    cls_ = Mapping
16
    if hasattr(cls, '__args__'):
17
        cls_ = MappingType[cls.__args__]
18
    dict_ = default_dict_deserializer(obj, cls_, **kwargs)
19
    result = dict_
20
    # Strip any generics from cls to allow for an instance check.
21
    stripped_cls = getattr(cls, '__extra__', cls)
22
    if not isinstance(result, stripped_cls):
23
        result = cls(dict_)
24
    return result
25