Completed
Push — master ( a9727a...22e3f6 )
by Ramon
11s queued 10s
created

jsons.deserializers.default_iterable   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 17
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_iterable_deserializer() 0 24 3
1
from collections import Mapping, Iterable
2
from typing import Iterable as IterableType
3
from jsons.deserializers import default_list_deserializer
4
5
6
def default_iterable_deserializer(
7
        obj: list,
8
        cls: type,
9
        **kwargs) -> Iterable:
10
    """
11
    Deserialize a (JSON) list into an ``Iterable`` by deserializing all items
12
    of that list. The given obj is assumed to be homogeneous; if the list has a
13
    generic type (e.g. Set[datetime]) then it is assumed that all elements can
14
    be deserialized to that type.
15
    :param obj: The list that needs deserializing to an ``Iterable``.
16
    :param cls: The type, optionally with a generic (e.g. Deque[str]).
17
    :param kwargs: Any keyword arguments.
18
    :return: A deserialized ``Iterable`` (e.g. ``set``) instance.
19
    """
20
    cls_ = Mapping
21
    if hasattr(cls, '__args__'):
22
        cls_ = IterableType[cls.__args__]
23
    list_ = default_list_deserializer(obj, cls_, **kwargs)
24
    result = list_
25
    # Strip any generics from cls to allow for an instance check.
26
    stripped_cls = getattr(cls, '__extra__', cls)
27
    if not isinstance(result, stripped_cls):
28
        result = stripped_cls(list_)
29
    return result
30