So my problem is That when I have a class of type A
that does things and I use those functions as a subclass(B)
they are still typed for class A
and do not accept my class B
object as arguments or as function signature.
My problem simplified:
from typing import TypeVar, Generic, Callable
T = TypeVar('T')
class Signal(Generic[T]):
def connect(self, connector: Callable[[T], None]) -> None:
pass
def emit(self, payload: T):
pass
class A:
def __init__(self) -> None:
self.signal = Signal[A]()
def do(self) -> None:
self.signal.emit(self)
def handle_b(b: "B") -> None:
print(b.something)
class B(A):
def __init__(self) -> None:
super().__init__()
self.signal.connect(handle_b)
@property
def something(self) -> int:
return 42
I can provide the complete signal class as well but that just distracts from the problem. This leaves me with one error in mypy:
error: Argument 1 to "connect" of "Signal" has incompatible type Callable[[B], None]; expected Callable[[A], None]
Since the signal handling is implemented in A
the subclass B
can't expect B
type objects to be returned even though it clearly should be fine...