[Python-checkins] peps: Short subsection on annotating coroutines (Ivan L, #225).
guido.van.rossum
python-checkins at python.org
Tue May 24 15:20:06 EDT 2016
https://hg.python.org/peps/rev/50c3f5aefbb7
changeset: 6341:50c3f5aefbb7
user: Guido van Rossum <guido at python.org>
date: Tue May 24 12:18:54 2016 -0700
summary:
Short subsection on annotating coroutines (Ivan L, #225).
files:
pep-0484.txt | 34 ++++++++++++++++++++++++++++++++++
1 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/pep-0484.txt b/pep-0484.txt
--- a/pep-0484.txt
+++ b/pep-0484.txt
@@ -1015,6 +1015,40 @@
ellipsis, i.e. the above example is literally what you would write.
+Annotating generator functions and coroutines
+---------------------------------------------
+
+The return type of generator functions can be annotated by
+the generic type ``Generator[yield_type, send_type,
+return_type]`` provided by ``typing.py`` module::
+
+ def echo_round() -> Generator[int, float, str]:
+ res = yield
+ while res:
+ res = yield round(res)
+ return 'OK'
+
+Coroutines introduced in PEP 492 are annotated with the same syntax as
+ordinary functions. However, the return type annotation corresponds to the
+type of ``await`` expression, not to the coroutine type::
+
+ async def spam(ignored: int) -> str:
+ return 'spam'
+
+ async def foo():
+ bar = await spam(42) # type: str
+
+The ``typing.py`` module also provides generic ABCs ``Awaitable``,
+``AsyncIterable``, and ``AsyncIterator`` for situations where more precise
+types cannot be specified::
+
+ def op() -> typing.Awaitable[str]:
+ if cond:
+ return spam(42)
+ else:
+ return asyncio.Future(...)
+
+
Compatibility with other uses of function annotations
=====================================================
--
Repository URL: https://hg.python.org/peps
More information about the Python-checkins
mailing list