How to properly type methods that return Generators with subclasses? #1036
-
I asked on from typing import Callable, Generator
class Parent:
def simple_method(self) -> "Parent":
...
def my_method(self, param: str) -> Generator["Parent", None, None]:
...
class Child(Parent):
simple_method: Callable[["Child"], "Child"]
my_method: Callable[["Child", str], Generator["Child", None, None]]
class OtherChild(Parent):
simple_method: Callable[["OtherChild"], "Child"]
my_method: Callable[["OtherChild", str], Generator["Child", None, None]] The For I'm not sure why this isn't allowed. Isn't it the same as returning |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The problem is that def my_method(self, param: str, /) -> Generator["Parent", None, None]:
... or def my_method(self, __param: str) -> Generator["Parent", None, None]:
... The error message provided by mypy is quite misleading and could be improved. By comparison, here's the error emitted by pyright:
-- |
Beta Was this translation helpful? Give feedback.
The problem is that
Callable
parameters are position-only, since they are nameless, but the methodmy_method
that you are attempting to override accepts a parameter namedparam
that can be specified as positional or as keyword. If you changemy_method
inParent
to accept position-only parameters, mypy will no longer generate an error.or
The error message provided by mypy is quite misleading and could be improved. By comparison, here's the error emitted by pyright: