Skip to content

Commit

Permalink
Also mention overloads in async iterator documentation (#14998)
Browse files Browse the repository at this point in the history
I added this section in #14973. Fixes #14996
  • Loading branch information
hauntsaninja authored Apr 3, 2023
1 parent a44afe1 commit 7d2844c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions docs/source/more_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -941,11 +941,11 @@ One common confusion is that the presence of a ``yield`` statement in an
await arange(5) # Error: Incompatible types in "await" (actual type "AsyncIterator[int]", expected type "Awaitable[Any]")
reveal_type(await coroutine(5)) # Revealed type is "typing.AsyncIterator[builtins.int]"
This can sometimes come up when trying to define base classes or Protocols:
This can sometimes come up when trying to define base classes, Protocols or overloads:

.. code-block:: python
from typing import AsyncIterator, Protocol
from typing import AsyncIterator, Protocol, overload
class LauncherIncorrect(Protocol):
# Because launch does not have yield, this has type
Expand All @@ -964,3 +964,17 @@ This can sometimes come up when trying to define base classes or Protocols:
raise NotImplementedError
if False:
yield 0
# The type of the overloads is independent of the implementation.
# In particular, their type is not affected by whether or not the
# implementation contains a `yield`.
# Use of `def`` makes it clear the type is Callable[..., AsyncIterator[int]],
# whereas with `async def` it would be Callable[..., Coroutine[Any, Any, AsyncIterator[int]]]
@overload
def launch(*, count: int = ...) -> AsyncIterator[int]: ...
@overload
def launch(*, time: float = ...) -> AsyncIterator[int]: ...
async def launch(*, count: int = 0, time: float = 0) -> AsyncIterator[int]:
# The implementation of launch is an async generator and contains a yield
yield 0

0 comments on commit 7d2844c

Please sign in to comment.