Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async generator breakage #489

Closed
scop opened this issue Jul 15, 2021 · 2 comments · Fixed by #491
Closed

async generator breakage #489

scop opened this issue Jul 15, 2021 · 2 comments · Fixed by #491
Labels

Comments

@scop
Copy link
Contributor

scop commented Jul 15, 2021

pyupgrade 2.21.0, python 3.8.0

import asyncio


async def agen():
    for x in range(5):
        yield x


async def main():
    x = tuple([i ** 2 async for i in agen()])
    print(x)


asyncio.run(main())
$ python t2.py
(0, 1, 4, 9, 16)

pyupgrade makes this change:

--- t2.py.orig	2021-07-15 22:19:32.201257932 +0300
+++ t2.py	2021-07-15 22:23:54.089120663 +0300
@@ -10 +10 @@
-    x = tuple([i ** 2 async for i in agen()])
+    x = tuple(i ** 2 async for i in agen())

...which breaks:

$ python t2.py
Traceback (most recent call last):
  File "t2.py", line 14, in <module>
    asyncio.run(main())
[...]
    x = tuple(i ** 2 async for i in agen())
TypeError: 'async_generator' object is not iterable
@MarcoGorelli
Copy link
Contributor

Thanks for the report - do you know if it's enough to just exclude list comprehensions which include async generators from the feature?

@asottile
Copy link
Owner

looks like you'd just skip if any of the generators have an is_async=1 comprehension:

$ astpretty --no-show-offsets /dev/stdin <<< 'async def f(): [1 async for x in ...]'
Module(
    body=[
        AsyncFunctionDef(
            name='f',
            args=arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
            body=[
                Expr(
                    value=ListComp(
                        elt=Num(n=1),
                        generators=[
                            comprehension(
                                target=Name(id='x', ctx=Store()),
                                iter=Ellipsis(),
                                ifs=[],
                                is_async=1,
                            ),
                        ],
                    ),
                ),
            ],
            decorator_list=[],
            returns=None,
        ),
    ],
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants