@@ -874,68 +874,6 @@ value of type :py:class:`Coroutine[Any, Any, T] <typing.Coroutine>`, which is a
874874 :ref: `reveal_type() <reveal-type >` displays the inferred static type of
875875 an expression.
876876
877- If you want to use coroutines in Python 3.4, which does not support
878- the ``async def `` syntax, you can instead use the :py:func: `@asyncio.coroutine <asyncio.coroutine> `
879- decorator to convert a generator into a coroutine.
880-
881- Note that we set the ``YieldType `` of the generator to be ``Any `` in the
882- following example. This is because the exact yield type is an implementation
883- detail of the coroutine runner (e.g. the :py:mod: `asyncio ` event loop) and your
884- coroutine shouldn't have to know or care about what precisely that type is.
885-
886- .. code-block :: python
887-
888- from typing import Any, Generator
889- import asyncio
890-
891- @asyncio.coroutine
892- def countdown_2 (tag : str , count : int ) -> Generator[Any, None , str ]:
893- while count > 0 :
894- print (' T-minus {} ({} )' .format(count, tag))
895- yield from asyncio.sleep(0.1 )
896- count -= 1
897- return " Blastoff!"
898-
899- loop = asyncio.get_event_loop()
900- loop.run_until_complete(countdown_2(" USS Enterprise" , 5 ))
901- loop.close()
902-
903- As before, the result of calling a generator decorated with :py:func: `@asyncio.coroutine <asyncio.coroutine> `
904- will be a value of type :py:class: `Awaitable[T] <typing.Awaitable> `.
905-
906- .. note ::
907-
908- At runtime, you are allowed to add the :py:func: `@asyncio.coroutine <asyncio.coroutine> ` decorator to
909- both functions and generators. This is useful when you want to mark a
910- work-in-progress function as a coroutine, but have not yet added ``yield `` or
911- ``yield from `` statements:
912-
913- .. code-block :: python
914-
915- import asyncio
916-
917- @asyncio.coroutine
918- def serialize (obj : object ) -> str :
919- # todo: add yield/yield from to turn this into a generator
920- return " placeholder"
921-
922- However, mypy currently does not support converting functions into
923- coroutines. Support for this feature will be added in a future version, but
924- for now, you can manually force the function to be a generator by doing
925- something like this:
926-
927- .. code-block :: python
928-
929- from typing import Generator
930- import asyncio
931-
932- @asyncio.coroutine
933- def serialize (obj : object ) -> Generator[None , None , str ]:
934- # todo: add yield/yield from to turn this into a generator
935- if False :
936- yield
937- return " placeholder"
938-
939877You may also choose to create a subclass of :py:class: `~typing.Awaitable ` instead:
940878
941879.. code-block :: python
@@ -995,11 +933,29 @@ To create an iterable coroutine, subclass :py:class:`~typing.AsyncIterator`:
995933 loop.run_until_complete(countdown_4(" Serenity" , 5 ))
996934 loop.close()
997935
998- For a more concrete example, the mypy repo has a toy webcrawler that
999- demonstrates how to work with coroutines. One version
1000- `uses async/await <https://github.com/python/mypy/blob/master/test-data/samples/crawl2.py >`_
1001- and one
1002- `uses yield from <https://github.com/python/mypy/blob/master/test-data/samples/crawl.py >`_.
936+ If you use coroutines in legacy code that was originally written for
937+ Python 3.4, which did not support the ``async def `` syntax, you would
938+ instead use the :py:func: `@asyncio.coroutine <asyncio.coroutine> `
939+ decorator to convert a generator into a coroutine, and use a
940+ generator type as the return type:
941+
942+ .. code-block :: python
943+
944+ from typing import Any, Generator
945+ import asyncio
946+
947+ @asyncio.coroutine
948+ def countdown_2 (tag : str , count : int ) -> Generator[Any, None , str ]:
949+ while count > 0 :
950+ print (' T-minus {} ({} )' .format(count, tag))
951+ yield from asyncio.sleep(0.1 )
952+ count -= 1
953+ return " Blastoff!"
954+
955+ loop = asyncio.get_event_loop()
956+ loop.run_until_complete(countdown_2(" USS Enterprise" , 5 ))
957+ loop.close()
958+
1003959
1004960 .. _typeddict :
1005961
0 commit comments