Код работает нормально, однако mypy жалуется, поскольку определяет тип параметра из родительского класса, а не из подкласса.
Код: Выделить всё
class ParentType:
def __init__(self, param_1: int):
self.param_1 = param_1
class ChildType(ParentType):
def __init__(self, param_1: int, param_2: int):
super().__init__(param_1=param_1)
self.param_2 = param_2
class Parent:
def __init__(self, a: ParentType):
self.a = a
class Child(Parent):
def __init__(self, a: ChildType):
super().__init__(a=a)
child_type = ChildType(param_1=1, param_2=2)
child = Child(a=child_type)
print(child.a.param_1) # works
print(child.a.param_2) # works, but mypy complains that "ParentType" has no attribute "param_2"; maybe "param_1"?
Подробнее здесь: https://stackoverflow.com/questions/778 ... class-of-t