asyncio REPL doesn't allow using asynchronous comprehensions outside of async func. Same behavior can also be observed when using ast.PyCF_ALLOW_TOP_LEVEL_AWAIT flag in compile()
Example with `async for`:
>>> asyncdefasync_gen():
... for x inrange(5):
... yieldawait asyncio.sleep(1, x)
...
>>> [x asyncfor x in async_gen()]
File "<console>", line 0
SyntaxError: asynchronous comprehension outside of an asynchronous function
Example with `await`:
>>> [await asyncio.sleep(1, x) for x inrange(5)]
File "<console>", line 0
SyntaxError: asynchronous comprehension outside of an asynchronous function
I also noticed that putting await before the async comprehension will make the code inside the comprehension run (though after it runs, it will fail on awaiting list):
>>> await [await asyncio.sleep(1, print(x)) for x inrange(5)]
0
1
2
3
4
Traceback (most recent call last):
File "C:\Python38\lib\concurrent\futures\_base.py", line 439, in result
return self.__get_result()
File "C:\Python38\lib\concurrent\futures\_base.py", line 388, in __get_result
raise self._exception
File "<console>", line 1, in <module>
TypeError: object list can't be used in 'await' expression
New changeset 9052f7a by Batuhan Taşkaya in branch 'master': bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968) 9052f7a
New changeset ec8a973 by Miss Islington (bot) in branch '3.8': bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968) ec8a973
Activity
jack1142 commented on Feb 5, 2020
asyncio REPL doesn't allow using asynchronous comprehensions outside of async func. Same behavior can also be observed when using ast.PyCF_ALLOW_TOP_LEVEL_AWAIT flag in compile()
jack1142 commented on Feb 5, 2020
I also noticed that putting await before the async comprehension will make the code inside the comprehension run (though after it runs, it will fail on awaiting list):
pablogsal commented on Mar 19, 2020
New changeset 9052f7a by Batuhan Taşkaya in branch 'master':
bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968)
9052f7a
miss-islington commented on Mar 19, 2020
New changeset ec8a973 by Miss Islington (bot) in branch '3.8':
bpo-39562: Allow executing asynchronous comprehensions in the asyncio REPL (GH-18968)
ec8a973
vstinner commented on Mar 30, 2020
This change caused python-gmpy2 tests to fail:
https://bugzilla.redhat.com/show_bug.cgi?id=1817710
Reproducer:
---
====
>>> all(x == 1 for x in [1, 1, 1]) True """.strip(), file=fp)27 remaining items
Carreau commented on Jul 6, 2020
This make non-await list comprehension coroutine-code-objects as well:
https://bugs.python.org/issue41218
This leads to weird things in Jupyter/IPython when we try to detect wether a block of code is, or os not async.
Carreau commented on Jul 6, 2020
Pablo, I see you reopened and marked as blocker,
As the changes here has been released maybe you want to reclose and marked https://bugs.python.org/issue41218 as blocker it should also be fixed by
#21357
pablogsal commented on Jul 6, 2020
Thanks for the heads up, Matthias!
Yes, I think is the best thing to do. For some reason, I thought this was not still released :(
asottile commented on Aug 24, 2021
this maybe shouldn't have been backported to 3.8.x as the change in compiler flags appears to break pyc files in subtle ways: https://stackoverflow.com/q/68910329/812183