1+ import asyncio
12import pickle
23import re
34import 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+
963994class 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 )
0 commit comments