My project depends on another project that stores the type annotations in stub files. Within the .py file the other project defines a base class that I need to inherit from a follows
# within a .py file
class Foo:
def bar(self, *baz):
raise NotImplementedError
In the corresponding .pyi stub they annotate it as follows:
# whitin a .pyi file
from typing import Generic, TypeVar, Callable
T_co = TypeVar("T_co", covariant=True)
class Foo(Generic[T_co]):
bar: Callable[..., T_co]
For my project I want to do the type annotations inline, i.e. in the .py file, and tried to do it in a subclass of Foo as follows:
# within a .py file
class SubFoo(Foo):
def bar(self, baz: float) -> str:
pass
Running mypy on this results in the following error
error: Signature of "bar" incompatible with supertype "Foo"
If I remove my inline annotations and add this to a .pyi stub
# within a .pyi file
class SubFoo(Foo):
bar: Callable[[float], str]
mypy runs fine.
I thought that both methods are equivalent, but apparently that is not the case. Can someone explain to me how they differ and what I need to change to make this work with inline annotations?
It became clear in the comments of @Michael0x2a answer that the error is only reproducible if you indeed use a .py and a .pyi file. You can download the examples from above here.