`yield from` combines single-type iterators
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
When using `yield from` on a variable of type `Iterable[a] | Iterable[b]`, the return type seems to combine the two single-type iterator possibilities into a generator `Generator[a | b, ...]`.
This causes a very basic direct wrapper as in the sample code below to fail type checking.
**Code or Screenshots**
```python
from typing import Iterable
def single_type_iterable() -> Iterable[int] | Iterable[str]:
raise NotImplementedError()
def wrapper() -> Iterable[int] | Iterable[str]:
yield from single_type_iterable() # ERROR
```
```
iter_demo.py:7:16 - error: Return type of generator function must be compatible with "Generator[int | str, Any, Any]"
Type "Generator[int | str, Unknown, Unknown]" is not assignable to type "Iterable[int] | Iterable[str]"
"Generator[int | str, Unknown, Unknown]" is not assignable to "Iterable[int]"
Type parameter "_T_co@Iterable" is covariant, but "int | str" is not a subtype of "int"
Type "int | str" is not assignable to type "int"
"str" is not assignable to "int"
"Generator[int | str, Unknown, Unknown]" is not assignable to "Iterable[str]"
Type parameter "_T_co@Iterable" is covariant, but "int | str" is not a subtype of "str"
Type "int | str" is not assignable to type "str"
... (reportReturnType)
1 error, 0 warnings, 0 informations
```
**VS Code extension or command-line**
Verified to reproduce in CLI v1.1.407 via npx/npm (plain call, no config provided)
Contributor guide
Research direction
Start by running the minimal example in iter_demo.py with the Pyright CLI and inspect how yield from handles the Iterable[int] | Iterable[str] return type. The work is done when this wrapper type-checks without the reported error and the behavior is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100