8000 Also mention overloads in async iterator documentation (#14998) · python/mypy@7d2844c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d2844c

Browse files
authored
Also mention overloads in async iterator documentation (#14998)
I added this section in #14973. Fixes #14996
1 parent a44afe1 commit 7d2844c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

docs/source/more_types.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,11 +941,11 @@ One common confusion is that the presence of a ``yield`` statement in an
941941
await arange(5) # Error: Incompatible types in "await" (actual type "AsyncIterator[int]", expected type "Awaitable[Any]")
942942
reveal_type(await coroutine(5)) # Revealed type is "typing.AsyncIterator[builtins.int]"
943943
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:
945945

946946
.. code-block:: python
947947
948-
from typing import AsyncIterator, Protocol
948+
from typing import AsyncIterator, Protocol, overload
949949
950950
class LauncherIncorrect(Protocol):
951951
# 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:
964964
raise NotImplementedError
965965
if False:
966966
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

Comments
 (0)
0