Passed
Push — master ( af9889...f446f0 )
by Max
01:14
created

structured_data._adt.annotations   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 24
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A sum_args_from_annotations() 0 8 2
A product_args_from_annotations() 0 10 4
A _all_annotations() 0 4 3
1
"""Helper functions for processing annotations."""
2
3
import sys
4
import typing
5
6
from .. import _nillable_write
7
from . import ctor
8
9
10
def _all_annotations(cls: type) -> typing.Iterator[typing.Tuple[type, str, typing.Any]]:
11
    for superclass in reversed(cls.__mro__):
12
        for key, value in vars(superclass).get("__annotations__", {}).items():
13
            yield (superclass, key, value)
14
15
16
def sum_args_from_annotations(cls: type) -> typing.Dict[str, typing.Tuple]:
17
    """Return the constructor data for Sum classes."""
18
    args: typing.Dict[str, typing.Tuple] = {}
19
    for superclass, key, value in _all_annotations(cls):
20
        _nillable_write.nillable_write(
21
            args, key, ctor.get_args(value, vars(sys.modules[superclass.__module__]))
22
        )
23
    return args
24
25
26
def product_args_from_annotations(cls: type) -> typing.Dict[str, typing.Any]:
27
    """Return the field data for Product classes."""
28
    args: typing.Dict[str, typing.Any] = {}
29
    for superclass, key, value in _all_annotations(cls):
30
        if value == "None" or ctor.annotation_is_classvar(
31
            value, vars(sys.modules[superclass.__module__])
32
        ):
33
            value = None
34
        _nillable_write.nillable_write(args, key, value)
35
    return args
36