Return type AsyncIterable
not correctly deduced for abstract methods
#17188
Labels
bug
mypy got something wrong
Bug Report
I am trying to write an abstract class (
Protocol
orabc.ABC
) with a method thatyield
s values. This concept works well fordef foo(self, args: list[T]) -> Iterable[T]: ...
, but breaks down forasync def foo(self, args: list[T]) -> AsyncIterable[T]: ...
. Ifyield
is added to the body of the async method, the return type is correctly deduced.In order to make the issue self documenting, I add a snippet from the playground example:
The error is
error: Return type "AsyncIterable[str]" of "get_strings" incompatible with return type "Coroutine[Any, Any, AsyncIterable[str]]" in supertype "MyABC" [override]
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=86f6de6b39e17f87e2c4e46b01aaebc2
Expected Behavior
No error, even without
yield
.Actual Behavior
The error is
error: Return type "AsyncIterable[str]" of "get_strings" incompatible with return type "Coroutine[Any, Any, AsyncIterable[str]]" in supertype "MyABC" [override]
Your Environment
The error is reproduced on mypy 1.10 on the Playground.
edit: I found #5385 which describes a very similar problem, maybe the same even. However, I am not too convinced with the proposed solution. I think it should be possible to write a
Protocol
orabc.ABC
just the way I would write a normal function.The text was updated successfully, but these errors were encountered: