Passed
Push — master ( c3e849...b2eec9 )
by Max
01:04
created

sum_args_from_annotations()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
"""Helper functions for processing annotations."""
2
3
import sys
4
import typing
5
6
from . import _ctor
7
from . import _nillable_write
8
9
_T = typing.TypeVar("_T")
10
11
12
def _all_annotations(
13
    cls: typing.Type[_T]
14
) -> typing.Iterator[typing.Tuple[typing.Type[_T], str, typing.Any]]:
15
    for superclass in reversed(cls.__mro__):
16
        for key, value in vars(superclass).get("__annotations__", {}).items():
17
            yield (superclass, key, value)
18
19
20
def sum_args_from_annotations(cls: typing.Type[_T]) -> typing.Dict[str, typing.Tuple]:
21
    """Return the constructor data for Sum classes."""
22
    args: typing.Dict[str, typing.Tuple] = {}
23
    for superclass, key, value in _all_annotations(cls):
24
        _nillable_write.nillable_write(
25
            args, key, _ctor.get_args(value, vars(sys.modules[superclass.__module__]))
26
        )
27
    return args
28
29
30
def product_args_from_annotations(
31
    cls: typing.Type[_T]
32
) -> typing.Dict[str, typing.Any]:
33
    """Return the field data for Product classes."""
34
    args: typing.Dict[str, typing.Any] = {}
35
    for superclass, key, value in _all_annotations(cls):
36
        if value == "None" or _ctor.annotation_is_classvar(
37
            value, vars(sys.modules[superclass.__module__])
38
        ):
39
            value = None
40
        _nillable_write.nillable_write(args, key, value)
41
    return args
42