Demo example:
===================
class MyType(type):
def __init__(cls, name, bases, nmspc):
if "__annotations__" in nmspc:
annotations = nmspc["__annotations__"]
else:
nmspc["__annotations__"] = {}
annotations = nmspc["__annotations__"]
for parent in bases:
base_annotations = (
parent.__annotations__ if hasattr(parent, "__annotations__") else {}
)
annotations.update(base_annotations)
super().__init__(name, bases, nmspc)
class Coordinate(metaclass=MyType):
x: float
y: float
class Address(metaclass=MyType):
street: str
country: str
class Location(Address, Coordinate):
pass
class Location2(Address, Coordinate):
name: str
print(Location.__annotations__)
print(Location2.__annotations__)
================
Output:
{'street': <class 'str'>, 'country': <class 'str'>}
{'name': <class 'str'>, 'street': <class 'str'>, 'country': <class 'str'>, 'x': <class 'float'>, 'y': <class 'float'>}
Was expecting the two print to be only different by 'name': <class 'str'> but the `Location fails to inherit the attribute from `Coordinate`. Not the case for `Location2`
*it's my first time submitting an issue, please kindly tell me what to do if I am not doing the right thing.
|