Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f17c200

Browse files
committed
Add Awaitable, AsyncIterable, AsyncIterator to typing.py.
1 parent b1f64e7 commit f17c200

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lib/test/test_typing.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import pickle
23
import re
34
import sys
@@ -960,6 +961,36 @@ def blah():
960961
pass
961962

962963

964+
T_a = TypeVar('T')
965+
966+
967+
class AwaitableWrapper(typing.Awaitable[T_a]):
968+
969+
def __init__(self, value):
970+
self.value = value
971+
972+
def __await__(self) -> typing.Iterator[T_a]:
973+
yield
974+
return self.value
975+
976+
977+
class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
978+
979+
def __init__(self, value: typing.Iterable[T_a]):
980+
self.value = value
981+
982+
def __aiter__(self) -> typing.AsyncIterator[T_a]:
983+
return self
984+
985+
@asyncio.coroutine
986+
def __anext__(self) -> T_a:
987+
data = yield from self.value
988+
if data:
989+
return data
990+
else:
991+
raise StopAsyncIteration
992+
993+
963994
class CollectionsAbcTests(TestCase):
964995

965996
def test_hashable(self):
@@ -984,6 +1015,36 @@ def test_iterator(self):
9841015
assert isinstance(it, typing.Iterator[int])
9851016
assert not isinstance(42, typing.Iterator)
9861017

1018+
def test_awaitable(self):
1019+
async def foo() -> typing.Awaitable[int]:
1020+
return await AwaitableWrapper(42)
1021+
g = foo()
1022+
assert issubclass(type(g), typing.Awaitable[int])
1023+
assert isinstance(g, typing.Awaitable)
1024+
assert not isinstance(foo, typing.Awaitable)
1025+
assert issubclass(typing.Awaitable[Manager],
1026+
typing.Awaitable[Employee])
1027+
assert not issubclass(typing.Awaitable[Employee],
1028+
typing.Awaitable[Manager])
1029+
g.send(None) # Run foo() till completion, to avoid warning.
1030+
1031+
def test_async_iterable(self):
1032+
base_it = range(10) # type: Iterator[int]
1033+
it = AsyncIteratorWrapper(base_it)
1034+
assert isinstance(it, typing.AsyncIterable)
1035+
assert isinstance(it, typing.AsyncIterable)
1036+
assert issubclass(typing.AsyncIterable[Manager],
1037+
typing.AsyncIterable[Employee])
1038+
assert not isinstance(42, typing.AsyncIterable)
1039+
1040+
def test_async_iterator(self):
1041+
base_it = range(10) # type: Iterator[int]
1042+
it = AsyncIteratorWrapper(base_it)
1043+
assert isinstance(it, typing.AsyncIterator)
1044+
assert issubclass(typing.AsyncIterator[Manager],
1045+
typing.AsyncIterator[Employee])
1046+
assert not isinstance(42, typing.AsyncIterator)
1047+
9871048
def test_sized(self):
9881049
assert isinstance([], typing.Sized)
9891050
assert not isinstance(42, typing.Sized)

Lib/typing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
# ABCs (from collections.abc).
3030
'AbstractSet', # collections.abc.Set.
31+
'Awaitable',
32+
'AsyncIterator',
33+
'AsyncIterable',
3134
'ByteString',
3235
'Container',
3336
'Hashable',
@@ -1261,6 +1264,18 @@ class _Protocol(metaclass=_ProtocolMeta):
12611264
Hashable = collections_abc.Hashable # Not generic.
12621265

12631266

1267+
class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1268+
__slots__ = ()
1269+
1270+
1271+
class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1272+
__slots__ = ()
1273+
1274+
1275+
class AsyncIterator(AsyncIterable[T_co], extra=collections_abc.AsyncIterator):
1276+
__slots__ = ()
1277+
1278+
12641279
class Iterable(Generic[T_co], extra=collections_abc.Iterable):
12651280
__slots__ = ()
12661281

0 commit comments

Comments
 (0)