Completed
Push — master ( 8f3758...257d3c )
by Ramon
29s queued 11s
created

jsons.deserializers.default_list._single_task()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
from multiprocessing import Process
2
3
from typish import get_args
4
5
from jsons._load_impl import load
6
from jsons._multitasking import multi_task
7
from jsons.exceptions import JsonsError
8
9
10
def default_list_deserializer(
11
        obj: list,
12
        cls: type = None,
13
        *,
14
        tasks: int = 1,
15
        task_type: type = Process,
16
        **kwargs) -> list:
17
    """
18
    Deserialize a list by deserializing all items of that list.
19
    :param obj: the list that needs deserializing.
20
    :param cls: the type optionally with a generic (e.g. List[str]).
21
    :param tasks: the allowed number of tasks (threads or processes).
22
    :param task_type: the type that is used for multitasking.
23
    :param kwargs: any keyword arguments.
24
    :return: a deserialized list instance.
25
    """
26
    cls_ = None
27
    kwargs_ = {**kwargs}
28
    cls_args = get_args(cls)
29
    if cls_args:
30
        cls_ = cls_args[0]
31
        # Mark the cls as 'inferred' so that later it is known where cls came
32
        # from and the precedence of classes can be determined.
33
        kwargs_['_inferred_cls'] = True
34
35
    if tasks == 1:
36
        result = [load(elem, cls=cls_, tasks=1, **kwargs_) for elem in obj]
37
    elif tasks > 1:
38
        result = multi_task(load, obj, tasks, task_type, cls_, **kwargs_)
39
    else:
40
        raise JsonsError('Invalid number of tasks: {}'.format(tasks))
41
    return result
42