Skip to content

Missing SyntaxError check for async for in lambda inside an async function #11158

@Juniormin123

Description

@Juniormin123

Description and reproduce

pyright, (and pylance in vscode), does not warn against SyntaxError for the following code:

import asyncio
import random
from collections.abc import Callable


async def async_gen(limit: int = 10):
    while True:
        if limit <= 0:
            return
        await asyncio.sleep(rtn := random.random())
        yield rtn
        limit -= 1


async def run_with_callback[U, T](u: U, callback: Callable[[U], T]) -> T:
    await asyncio.sleep(0)  # some async ops
    return callback(u)


async def test_async_for_in_lambda():
    result = await run_with_callback(
        async_gen(5),
        lambda ag: [x async for x in ag],  # <- fails to detect syntax error, raises in runtime
    )
    print(result)


# def run_with_callback_sync[U, T](u: U, callback: Callable[[U], T]) -> T:
#     return callback(u)
# 
# 
# def test_async_for_in_lambda_sync():
#     result = run_with_callback_sync(
#         async_gen(5),
#         lambda ag: [x async for x in ag],  # <- correctly detect syntax error, raises in runtime
#     )
#     print(result)


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

Runtime output:

python /tmp/test.py
  File "/tmp/test.py", line 23
    lambda ag: [x async for x in ag],  # <- fails to detect syntax error, raises in runtime
               ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: asynchronous comprehension outside of an asynchronous function

pyright output:

 ➜ pyright /tmp/test.py
0 errors, 0 warnings, 0 informations

Related versions

  • pyright: 1.1.407
  • pylance: 2025.10.4
  • python: Python 3.14.0, conda distributed
  • OS: should be irrelevant, as this is pure Python

Note

May be this can be related to #9414

Metadata

Metadata

Assignees

No one assigned

    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