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

jsons.deserializers.default_mapping   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 26
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_mapping_deserializer() 0 20 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