jsons.deserializers.default_mapping   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 15
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_mapping_deserializer() 0 19 3
1
from collections.abc import Mapping
2
from typing import Mapping as MappingType
3
4
from typish import get_args, get_origin
5
6
from jsons.deserializers.default_dict import default_dict_deserializer
7
8
9
def default_mapping_deserializer(obj: dict, cls: type, **kwargs) -> Mapping:
10
    """
11
    Deserialize a (JSON) dict into a mapping by deserializing all items of that
12
    dict.
13
    :param obj: the dict that needs deserializing.
14
    :param cls: the type, optionally with a generic (e.g. Set[str]).
15
    :param kwargs: any keyword arguments.
16
    :return: a deserialized set instance.
17
    """
18
    cls_ = Mapping
19
    cls_args = get_args(cls)
20
    if cls_args:
21
        cls_ = MappingType[cls_args]
22
    dict_ = default_dict_deserializer(obj, cls_, **kwargs)
23
    result = dict_
24
    # Strip any generics from cls to allow for an instance check.
25
    if not isinstance(result, get_origin(cls)):
26
        result = cls(dict_)
27
    return result
28