microsoft / microsoft/pyright

Missing SyntaxError check for `async for` in lambda inside an async function

Open
#11,158 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

# Description and reproduce

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

```python
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
➜ 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:

```bash
➜ 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

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied reproduction with pyright and compare it with the synchronous lambda case. Trace the syntax or semantic analysis that determines whether an asynchronous comprehension is inside an async function. Done means pyright reports the SyntaxError for the async lambda case while preserving the existing valid-case behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.