Passed
Push — master ( ce73ec...38d9d4 )
by Max
01:00
created

structured_data._annotations   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 9

3 Functions

Rating   Name   Duplication   Size   Complexity  
A _all_annotations() 0 6 3
A _sum_args_from_annotations() 0 7 2
A _product_args_from_annotations() 0 11 4
1
import sys
2
import typing
3
4
from . import _ctor
5
from . import _nillable_write
6
7
_T = typing.TypeVar("_T")
8
9
10
def _all_annotations(
11
    cls: typing.Type[_T]
12
) -> typing.Iterator[typing.Tuple[typing.Type[_T], str, typing.Any]]:
13
    for superclass in reversed(cls.__mro__):
14
        for key, value in vars(superclass).get("__annotations__", {}).items():
15
            yield (superclass, key, value)
16
17
18
def _sum_args_from_annotations(cls: typing.Type[_T]) -> typing.Dict[str, typing.Tuple]:
19
    args: typing.Dict[str, typing.Tuple] = {}
20
    for superclass, key, value in _all_annotations(cls):
21
        _nillable_write.nillable_write(
22
            args, key, _ctor.get_args(value, vars(sys.modules[superclass.__module__]))
23
        )
24
    return args
25
26
27
def _product_args_from_annotations(
28
    cls: typing.Type[_T]
29
) -> typing.Dict[str, typing.Any]:
30
    args: typing.Dict[str, typing.Any] = {}
31
    for superclass, key, value in _all_annotations(cls):
32
        if value == "None" or _ctor.annotation_is_classvar(
33
            value, vars(sys.modules[superclass.__module__])
34
        ):
35
            value = None
36
        _nillable_write.nillable_write(args, key, value)
37
    return args
38