Conditions | 3 |
Total Lines | 24 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | from collections import Mapping, Iterable |
||
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 |