1
|
|
|
"""Helper functions for processing annotations.""" |
2
|
|
|
|
3
|
|
|
import inspect |
4
|
|
|
import sys |
5
|
|
|
import typing |
6
|
|
|
|
7
|
|
|
from .. import _nillable_write |
8
|
|
|
from . import ctor |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def _all_annotations(cls: type) -> typing.Iterator[typing.Tuple[type, str, typing.Any]]: |
12
|
|
|
for superclass in reversed(cls.__mro__): |
13
|
|
|
for key, value in vars(superclass).get("__annotations__", {}).items(): |
14
|
|
|
yield (superclass, key, value) |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def sum_args_from_annotations(cls: type) -> typing.Dict[str, typing.Tuple]: |
18
|
|
|
"""Return the constructor data for Sum classes.""" |
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(cls: type) -> typing.Dict[str, typing.Any]: |
28
|
|
|
"""Return the field data for Product classes.""" |
29
|
|
|
args: typing.Dict[str, typing.Any] = {} |
30
|
|
|
for superclass, key, value in _all_annotations(cls): |
31
|
|
|
if ( |
32
|
|
|
value == "None" |
33
|
|
|
or ctor.annotation_is_classvar( |
34
|
|
|
value, vars(sys.modules[superclass.__module__]) |
35
|
|
|
) |
36
|
|
|
or inspect.isdatadescriptor(inspect.getattr_static(cls, key, None)) |
37
|
|
|
): |
38
|
|
|
value = None |
39
|
|
|
_nillable_write.nillable_write(args, key, value) |
40
|
|
|
return args |
41
|
|
|
|