bpo-33346: Allow async comprehensions inside implicit async comprehen… · python/cpython@054e9c8 (original) (raw)

`@@ -28,6 +28,12 @@ def await(self):

`

28

28

`yield self.value

`

29

29

``

30

30

``

``

31

`+

async def asynciter(iterable):

`

``

32

`+

"""Convert an iterable to an asynchronous iterator."""

`

``

33

`+

for x in iterable:

`

``

34

`+

yield x

`

``

35

+

``

36

+

31

37

`def run_async(coro):

`

32

38

`assert coro.class in {types.GeneratorType, types.CoroutineType}

`

33

39

``

`@@ -125,6 +131,11 @@ def bar():

`

125

131

` for c in b]

`

126

132

` """,

`

127

133

``

``

134

`+

"""async def foo():

`

``

135

`+

def bar():

`

``

136

`+

[[async for i in b] for b in els]

`

``

137

`+

""",

`

``

138

+

128

139

`"""async def foo():

`

129

140

` def bar():

`

130

141

` [i for i in els

`

`@@ -200,6 +211,13 @@ def bar():

`

200

211

` [i for i in els if await i]

`

201

212

` """,

`

202

213

``

``

214

`+

"""def bar():

`

``

215

`+

[[i async for i in a] for a in elts]

`

``

216

`+

""",

`

``

217

+

``

218

`+

"""[[i async for i in a] for a in elts]

`

``

219

`+

""",

`

``

220

+

203

221

`"""async def foo():

`

204

222

` await

`

205

223

` """,

`

`@@ -2011,6 +2029,60 @@ async def f():

`

2011

2029

`run_async(f()),

`

2012

2030

` ([], {1: 1, 2: 2, 3: 3}))

`

2013

2031

``

``

2032

`+

def test_nested_comp(self):

`

``

2033

`+

async def run_list_inside_list():

`

``

2034

`+

return [[i + j async for i in asynciter([1, 2])] for j in [10, 20]]

`

``

2035

`+

self.assertEqual(

`

``

2036

`+

run_async(run_list_inside_list()),

`

``

2037

`+

([], [[11, 12], [21, 22]]))

`

``

2038

+

``

2039

`+

async def run_set_inside_list():

`

``

2040

`+

return [{i + j async for i in asynciter([1, 2])} for j in [10, 20]]

`

``

2041

`+

self.assertEqual(

`

``

2042

`+

run_async(run_set_inside_list()),

`

``

2043

`+

([], [{11, 12}, {21, 22}]))

`

``

2044

+

``

2045

`+

async def run_list_inside_set():

`

``

2046

`+

return {sum([i async for i in asynciter(range(j))]) for j in [3, 5]}

`

``

2047

`+

self.assertEqual(

`

``

2048

`+

run_async(run_list_inside_set()),

`

``

2049

`+

([], {3, 10}))

`

``

2050

+

``

2051

`+

async def run_dict_inside_dict():

`

``

2052

`+

return {j: {i: i + j async for i in asynciter([1, 2])} for j in [10, 20]}

`

``

2053

`+

self.assertEqual(

`

``

2054

`+

run_async(run_dict_inside_dict()),

`

``

2055

`+

([], {10: {1: 11, 2: 12}, 20: {1: 21, 2: 22}}))

`

``

2056

+

``

2057

`+

async def run_list_inside_gen():

`

``

2058

`+

gen = ([i + j async for i in asynciter([1, 2])] for j in [10, 20])

`

``

2059

`+

return [x async for x in gen]

`

``

2060

`+

self.assertEqual(

`

``

2061

`+

run_async(run_list_inside_gen()),

`

``

2062

`+

([], [[11, 12], [21, 22]]))

`

``

2063

+

``

2064

`+

async def run_gen_inside_list():

`

``

2065

`+

gens = [(i async for i in asynciter(range(j))) for j in [3, 5]]

`

``

2066

`+

return [x for g in gens async for x in g]

`

``

2067

`+

self.assertEqual(

`

``

2068

`+

run_async(run_gen_inside_list()),

`

``

2069

`+

([], [0, 1, 2, 0, 1, 2, 3, 4]))

`

``

2070

+

``

2071

`+

async def run_gen_inside_gen():

`

``

2072

`+

gens = ((i async for i in asynciter(range(j))) for j in [3, 5])

`

``

2073

`+

return [x for g in gens async for x in g]

`

``

2074

`+

self.assertEqual(

`

``

2075

`+

run_async(run_gen_inside_gen()),

`

``

2076

`+

([], [0, 1, 2, 0, 1, 2, 3, 4]))

`

``

2077

+

``

2078

`+

async def run_list_inside_list_inside_list():

`

``

2079

`+

return [[[i + j + k async for i in asynciter([1, 2])]

`

``

2080

`+

for j in [10, 20]]

`

``

2081

`+

for k in [100, 200]]

`

``

2082

`+

self.assertEqual(

`

``

2083

`+

run_async(run_list_inside_list_inside_list()),

`

``

2084

`+

([], [[[111, 112], [121, 122]], [[211, 212], [221, 222]]]))

`

``

2085

+

2014

2086

`def test_copy(self):

`

2015

2087

`async def func(): pass

`

2016

2088

`coro = func()

`