Код: Выделить всё
class Person:
number: int
def __init__(self) -> None:
self.number = 0
def __add__(self, other: 'Person') -> 'Person':
self.number = self.number + other.number
return self
class Dan(Person):
def __init__(self) -> None:
super().__init__()
def __add__(self, other: 'Dan') -> 'Dan':
self.number = self.number + other.number + 1
return self
Код: Выделить всё
test.py:15: error: Argument 1 of "__add__" is incompatible with supertype "Person"; supertype defines the argument type as "Person" [override]
test.py:15: note: This violates the Liskov substitution principle
test.py:15: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
Моей первой мыслью о решении было создать переменную типа person_co = TypeVar("person_co",bound=Person). Но это не сработало, поскольку Person еще не был определен. Я также попробовал создать аргумент и тип возвращаемого значения Person для метода __add__ в Dan. Это сработало, и MyPy не выдал ошибок, но это неправильно, верно?
Подробнее здесь: https://stackoverflow.com/questions/752 ... or-when-ch
Мобильная версия