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
|
|
|
|