Mypy does not warn when breaking the abstract methods type specification in subtype's method [closed]
After running mypy against the above code, mypy does not warn anything. However, I think it should warn about type signature of AppleJuiceFactorty.__call__
method, because AppleJuiceFactory
inherits from JuiceFactory[Apple, AppleJuice]
therefore FluitT
and FruitJuiceT
are narrowed down to Apple
and AppleJuice
respectively.
Could you tell me what's the problem? And is it in my side or mypy side?
from abc import ABC, abstractmethod
from typing import Generic, TypeVar, Type
class Fruit: pass
class Apple(Fruit): pass
class Orange(Fruit): pass
FruitT = TypeVar('FruitT', bound=Fruit)
class FruitJuice: pass
class AppleJuice(FruitJuice): pass
class OrangeJuice(FruitJuice): pass
FruitJuiceT = TypeVar('FruitJuiceT', bound=FruitJuice)
JuiceFactoryT = TypeVar('JuiceFactoryT', bound='JuiceFactory')
class JuiceFactory(ABC, Generic[FruitT, FruitJuiceT]):
@abstractmethod
def __cal__(self, inp: FruitT) -> FruitJuiceT: pass
class AppleJuiceFactorty(JuiceFactory[Apple, AppleJuice]):
def __call__(self, inp: Orange) -> OrangeJuice:
return OrangeJuice()
source https://stackoverflow.com/questions/71982278/mypy-does-not-warn-when-breaking-the-abstract-methods-type-specification-in-subt
Comments
Post a Comment