1
+ import asyncio
1
2
import pickle
2
3
import re
3
4
import sys
@@ -959,6 +960,34 @@ def blah():
959
960
pass
960
961
961
962
963
+ T_a = TypeVar ('T' )
964
+
965
+
966
+ class AwaitableWrapper (typing .Awaitable [T_a ]):
967
+ def __init__ (self , value ):
968
+ self .value = value
969
+
970
+ def __await__ (self ) -> typing .Iterator [T_a ]:
971
+ yield
972
+ return self .value
973
+
974
+
975
+ class AsyncIteratorWrapper (typing .AsyncIterator [T_a ]):
976
+ def __init__ (self , value : typing .Iterable [T_a ]):
977
+ self .value = value
978
+
979
+ def __aiter__ (self ) -> typing .AsyncIterator [T_a ]:
980
+ return self
981
+
982
+ @asyncio .coroutine
983
+ def __anext__ (self ) -> T_a :
984
+ data = yield from self .value
985
+ if data :
986
+ return data
987
+ else :
988
+ raise StopAsyncIteration
989
+
990
+
962
991
class CollectionsAbcTests (TestCase ):
963
992
964
993
def test_hashable (self ):
@@ -983,6 +1012,35 @@ def test_iterator(self):
983
1012
assert isinstance (it , typing .Iterator [int ])
984
1013
assert not isinstance (42 , typing .Iterator )
985
1014
1015
+ def test_awaitable (self ):
1016
+ async def foo () -> typing .Awaitable [int ]:
1017
+ return await AwaitableWrapper (42 )
1018
+ g = foo ()
1019
+ assert issubclass (type (g ), typing .Awaitable [int ])
1020
+ assert isinstance (g , typing .Awaitable )
1021
+ assert not isinstance (foo , typing .Awaitable )
1022
+ assert issubclass (typing .Awaitable [Manager ],
1023
+ typing .Awaitable [Employee ])
1024
+ assert not issubclass (typing .Awaitable [Employee ],
1025
+ typing .Awaitable [Manager ])
1026
+
1027
+ def test_async_iterable (self ):
1028
+ base_it = range (10 ) # type: Iterator[int]
1029
+ it = AsyncIteratorWrapper (base_it )
1030
+ assert isinstance (it , typing .AsyncIterable )
1031
+ assert isinstance (it , typing .AsyncIterable )
1032
+ assert issubclass (typing .AsyncIterable [Manager ],
1033
+ typing .AsyncIterable [Employee ])
1034
+ assert not isinstance (42 , typing .AsyncIterable )
1035
+
1036
+ def test_async_iterator (self ):
1037
+ base_it = range (10 ) # type: Iterator[int]
1038
+ it = AsyncIteratorWrapper (base_it )
1039
+ assert isinstance (it , typing .AsyncIterator )
1040
+ assert issubclass (typing .AsyncIterator [Manager ],
1041
+ typing .AsyncIterator [Employee ])
1042
+ assert not isinstance (42 , typing .AsyncIterator )
1043
+
986
1044
def test_sized (self ):
987
1045
assert isinstance ([], typing .Sized )
988
1046
assert not isinstance (42 , typing .Sized )
0 commit comments