Skip to content

F704 wrong "await outside of a function" when using generators #21750

@PabloRuizCuevas

Description

@PabloRuizCuevas

Summary

Describe the Bug

Await can be outside of a function if is in code that won't be executed automatically (ex: async generators).

await statement outside of a function

This code is correct, yet ruff complains:

import asyncio

async def test2():
    return "test"

async def a_gen():
    for i in range(3):
        yield test2()

a = (await cor async for cor in a_gen()) # `await` statement outside of a function F704

async def main():
    async for i in a:
        print(i)

if __name__ == "__main__":
    asyncio.run(main())

I suggest changing F704 so awaits are ok inside async generators that are basically syntax sugar of a function.

of course for now we can do the equivalent:

async def a():
    for cor in a_gen():
        yield await cor

Version

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions