`--createstub` creates incorrect stub for async generator functions
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
I'm using Pyright 1.1.407 with Python 3.14. I'm running `pyright --createstub main` on this `main.py` file:
```py
from collections.abc import AsyncGenerator
async def foo() -> int:
return 1
async def bar() -> AsyncGenerator[int]:
yield 2
yield 3
```
this is the stub generated by pyright:
```py
"""
This type stub file was generated by pyright.
"""
from collections.abc import AsyncGenerator
async def foo() -> int:
...
async def bar() -> AsyncGenerator[int]:
...
```
The declaration of `bar` does not match the original file: it means that `bar` is a function returning `Coroutine[Any, Any, AsyncGenerator[int]]`. I believe the correct declaration of `bar` would be:
```py
def bar() -> AsyncGenerator[int]:
...
```
Additionally, if type annotations are omitted, pyright inserts a hint for `Generator` and not `AsyncGenerator`. It also marks `bar` as a coroutine function, not an async generator function — seems like there's no way to do it in `pyi` files besides something like `def bar() -> AsyncGenerator[Any, Any]:` (or just a bare `def bar():` as pyright does with non-async generator functions):
```py
async def foo(): # -> Literal[1]:
...
async def bar(): # -> Generator[Literal[2, 3], Any, None]:
...
```
Contributor guide
Research direction
Start with the --createstub command using the main.py example from the issue and compare the generated stub for bar with the expected declaration. Trace how async generator functions and omitted annotations are represented in generated stubs; done means bar is emitted as a non-async function returning AsyncGenerator and inferred hints use AsyncGenerator where appropriate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100