bpo-31708: Allow async generator expressions in synchronous functions… · pythoncapi/cpython@b8ab9d3 · GitHub
Skip to content

Commit b8ab9d3

Browse files
author
Yury Selivanov
authored
bpo-31708: Allow async generator expressions in synchronous functions (python#3905)
1 parent faa135a commit b8ab9d3

5 files changed

Lines changed: 52 additions & 9 deletions

File tree

Doc/reference/expressions.rst

Lines changed: 10 additions & 8 deletions

Lib/test/test_asyncgen.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,5 +1037,37 @@ async def wait():
10371037
t.cancel()
10381038
self.loop.run_until_complete(asyncio.sleep(0.1, loop=self.loop))
10391039

1040+
def test_async_gen_expression_01(self):
1041+
async def arange(n):
1042+
for i in range(n):
1043+
await asyncio.sleep(0.01, loop=self.loop)
1044+
yield i
1045+
1046+
def make_arange(n):
1047+
# This syntax is legal starting with Python 3.7
1048+
return (i * 2 async for i in arange(n))
1049+
1050+
async def run():
1051+
return [i async for i in make_arange(10)]
1052+
1053+
res = self.loop.run_until_complete(run())
1054+
self.assertEqual(res, [i * 2 for i in range(10)])
1055+
1056+
def test_async_gen_expression_02(self):
1057+
async def wrap(n):
1058+
await asyncio.sleep(0.01, loop=self.loop)
1059+
return n
1060+
1061+
def make_arange(n):
1062+
# This syntax is legal starting with Python 3.7
1063+
return (i * 2 for i in range(n) if await wrap(i))
1064+
1065+
async def run():
1066+
return [i async for i in make_arange(10)]
1067+
1068+
res = self.loop.run_until_complete(run())
1069+
self.assertEqual(res, [i * 2 for i in range(1, 10)])
1070+
1071+
10401072
if __name__ == "__main__":
10411073
unittest.main()

Lib/test/test_coroutines.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def bar():
149149
[i async for i in els]
150150
""",
151151

152+
"""def bar():
153+
{i: i async for i in els}
154+
""",
155+
156+
"""def bar():
157+
{i async for i in els}
158+
""",
159+
152160
"""def bar():
153161
[await i for i in els]
154162
""",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow use of asynchronous generator expressions in synchronous functions.

Python/compile.c

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)