I have a class Animal with a method foo, whose return type depends on the value of inplace.
Likewise, for the subclass Cat.
So far, so good. But, if I call super().foo in Cat, I get an error.
from __future__ import annotations
from typing import Optional, overload, Literal, TypeVar
CatOrDog = TypeVar('CatOrDog', bound='Animal')
class Animal:
@overload
def foo(self: CatOrDog, inplace: Literal[False]=...) -> CatOrDog: ...
@overload
def foo(self: CatOrDog, inplace: Literal[True]) -> None: ...
def foo(
self: CatOrDog, inplace: bool = False
) -> Optional[CatOrDog]:
...
class Cat(Animal):
@overload
def foo(self, inplace: Literal[False]=...) -> Cat: ...
@overload
def foo(self, inplace: Literal[True]) -> None: ...
def foo(self, inplace: bool = False):
return super().foo(inplace=inplace)
main.py:31: error: No overload variant of "foo" of "Animal" matches argument type "bool"
main.py:31: note: Possible overload variants:
main.py:31: note: def foo(self, inplace: Literal[False] = ...) -> Cat
main.py:31: note: def foo(self, inplace: Literal[True]) -> None
Found 1 error in 1 file (checked 1 source file)
https://mypy-play.net/?mypy=latest&python=3.9&gist=49da369f6343543769eed2060fa61639
I don't understand why it would fail - Animal does have an overload variant of foo which matches argument type bool.