Idea:
#!/usr/bin/env python3
def updateAnnotations(annotations, bases):
for base in bases:
annotations.update(getattr(base, '__annotations__', dict()))
class A:
valA: int
class B:
valB: int
class C(A, B):
valC: int
updateAnnotations(__annotations__, [A, B])
print(sorted(C.__annotations__.items()))
Output:
[('valA', <class 'int'>), ('valB', <class 'int'>), ('valC', <class 'int'>)]
Using a decorator:
def update_annotations(cls: typing.Type) -> typing.Type:
annotations = getattr(cls, '__annotations__', None)
if annotations == None:
cls.__annotations__ = dict()
annotations = cls.__annotations__
for clsType in [cls] + list(cls.__bases__):
annotations.update(getattr(clsType, '__annotations__', dict()))
return cls
class A:
valA: int
class B:
valA: str
valB: int
@update_annotations
class C(A, B):
valC: int
def __init__(self):
self.valA = 'asd'
self.valB = 30
self.valC = 40
print(C.__annotations__)
Output:
{'valC': <class 'int'>, 'valA': <class 'str'>, 'valB': <class 'int'>}