33from graphql .execution import map_async_iterable
44
55
6- async def map_doubles (x ):
6+ try : # pragma: no cover
7+ anext
8+ except NameError : # pragma: no cover (Python < 3.10)
9+ # noinspection PyShadowingBuiltins
10+ async def anext (iterator ):
11+ """Return the next item from an async iterator."""
12+ return await iterator .__anext__ ()
13+
14+
15+ async def map_doubles (x : int ) -> int :
716 return x + x
817
918
1019def describe_map_async_iterable ():
1120 @mark .asyncio
12- async def test_inner_close_called ():
13- """
14- Test that a custom iterator with aclose() gets an aclose() call
15- when outer is closed
16- """
17-
21+ async def inner_is_closed_when_outer_is_closed ():
1822 class Inner :
1923 def __init__ (self ):
2024 self .closed = False
@@ -30,19 +34,14 @@ async def __anext__(self):
3034
3135 inner = Inner ()
3236 outer = map_async_iterable (inner , map_doubles )
33- it = outer .__aiter__ ()
34- assert await it . __anext__ ( ) == 2
37+ iterator = outer .__aiter__ ()
38+ assert await anext ( iterator ) == 2
3539 assert not inner .closed
3640 await outer .aclose ()
3741 assert inner .closed
3842
3943 @mark .asyncio
40- async def test_inner_close_called_on_callback_err ():
41- """
42- Test that a custom iterator with aclose() gets an aclose() call
43- when the callback errors and the outer iterator aborts.
44- """
45-
44+ async def inner_is_closed_on_callback_error ():
4645 class Inner :
4746 def __init__ (self ):
4847 self .closed = False
@@ -62,17 +61,11 @@ async def callback(v):
6261 inner = Inner ()
6362 outer = map_async_iterable (inner , callback )
6463 with raises (RuntimeError ):
65- async for _ in outer :
66- pass
64+ await anext (outer )
6765 assert inner .closed
6866
6967 @mark .asyncio
70- async def test_inner_exit_on_callback_err ():
71- """
72- Test that a custom iterator with aclose() gets an aclose() call
73- when the callback errors and the outer iterator aborts.
74- """
75-
68+ async def test_inner_exits_on_callback_error ():
7669 inner_exit = False
7770
7871 async def inner ():
@@ -88,6 +81,35 @@ async def callback(v):
8881
8982 outer = map_async_iterable (inner (), callback )
9083 with raises (RuntimeError ):
91- async for _ in outer :
92- pass
84+ await anext (outer )
9385 assert inner_exit
86+
87+ @mark .asyncio
88+ async def inner_has_no_close_method_when_outer_is_closed ():
89+ class Inner :
90+ def __aiter__ (self ):
91+ return self
92+
93+ async def __anext__ (self ):
94+ return 1
95+
96+ outer = map_async_iterable (Inner (), map_doubles )
97+ iterator = outer .__aiter__ ()
98+ assert await anext (iterator ) == 2
99+ await outer .aclose ()
100+
101+ @mark .asyncio
102+ async def inner_has_no_close_method_on_callback_error ():
103+ class Inner :
104+ def __aiter__ (self ):
105+ return self
106+
107+ async def __anext__ (self ):
108+ return 1
109+
110+ async def callback (v ):
111+ raise RuntimeError ()
112+
113+ outer = map_async_iterable (Inner (), callback )
114+ with raises (RuntimeError ):
115+ await anext (outer )
0 commit comments