Но правила наследования меня сбивают с толку. Я хотел бы наследовать от класса, который определяет параметр типа, но заменить параметр типа родительского элемента другим классом.
Вот пример:
< pre class="lang-py Prettyprint-override">
Код: Выделить всё
class Foo[T]:
prop: T
def get_prop(self) -> T:
return self.prop
class StringFoo(Foo[str]):
prop: str = "Hello world"
def do_thing(self):
reveal_type(self.get_prop()) # str
class IntFoo(Foo[int]):
prop: int = 42
@property
def is_a_number(self) -> bool:
return True
def do_thing(self):
reveal_type(self.get_prop()) # int
Код: Выделить всё
class FloatFoo(IntFoo[float]): # this line has the message
def do_thing(self) -> float:
reveal_type(self.get_prop()) # int :(
return 42.0
Не ожидается никаких аргументов типа для класса «IntFoo»
Я даже пробовал это:
Код: Выделить всё
class FloatFoo(Foo[float], IntFoo): # there's another message here
prop: float = 42.0
def do_thing(self):
reveal_type(self.get_prop()) # float :)
Базовые классы FloatFoo несовместимы
< /blockquote>
Так как же я могу объявить подкласс IntFoo, который имеет все методы IntFoo, но имеет другой параметр типа?
Подробнее здесь: https://stackoverflow.com/questions/790 ... its-parent