← Back to index

annotations_coroutines.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests for annotating coroutines.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/annotations.html#annotating-generator-functions-and-coroutines
6
7# > Coroutines introduced in PEP 492 are annotated with the same syntax as
8# > ordinary functions. However, the return type annotation corresponds to
9# > the type of await expression, not to the coroutine type.
12from typing import Any, Callable, Coroutine, assert_type
15async def func1(ignored: int, /) -> str:
16 return "spam"
19# Don't use assert_type here because some type checkers infer
20# the narrower type types.CoroutineType rather than typing.Coroutine
21# in this case.
22v1: Callable[[int], Coroutine[Any, Any, str]] = func1
25async def func2() -> None:
26 x = await func1(42)
27 assert_type(x, str)