@@ -941,11 +941,11 @@ One common confusion is that the presence of a ``yield`` statement in an
941
941
await arange(5 ) # Error: Incompatible types in "await" (actual type "AsyncIterator[int]", expected type "Awaitable[Any]")
942
942
reveal_type(await coroutine(5 )) # Revealed type is "typing.AsyncIterator[builtins.int]"
943
943
944
- This can sometimes come up when trying to define base classes or Protocols :
944
+ This can sometimes come up when trying to define base classes, Protocols or overloads :
945
945
946
946
.. code-block :: python
947
947
948
- from typing import AsyncIterator, Protocol
948
+ from typing import AsyncIterator, Protocol, overload
949
949
950
950
class LauncherIncorrect (Protocol ):
951
951
# Because launch does not have yield, this has type
@@ -964,3 +964,17 @@ This can sometimes come up when trying to define base classes or Protocols:
964
964
raise NotImplementedError
965
965
if False :
966
966
yield 0
967
+
968
+ # The type of the overloads is independent of the implementation.
969
+ # In particular, their type is not affected by whether or not the
970
+ # implementation contains a `yield`.
971
+ # Use of `def`` makes it clear the type is Callable[..., AsyncIterator[int]],
972
+ # whereas with `async def` it would be Callable[..., Coroutine[Any, Any, AsyncIterator[int]]]
973
+ @overload
974
+ def launch (* , count : int = ... ) -> AsyncIterator[int ]: ...
975
+ @overload
976
+ def launch (* , time : float = ... ) -> AsyncIterator[int ]: ...
977
+
978
+ async def launch (* , count : int = 0 , time : float = 0 ) -> AsyncIterator[int ]:
979
+ # The implementation of launch is an async generator and contains a yield
980
+ yield 0
0 commit comments