microsoft / microsoft/pyright

exponential slowdown when using `yield from` with type alias

Open
#11,067 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

**Describe the bug**

if multiple function use `yield from` with a type alias type, processing time grows almost exponentially with the number of such functions.

**Code or Screenshots**

```python
#!/usr/bin/env python

from collections.abc import Iterator

class A[T]:
def __iter__(self) -> Iterator[T]: ...

class B[T]:
def __iter__(self) -> Iterator[T]: ...

type C[T] = A[T] | B[T]

def g() -> C[int | str]: ...

def f_0(): yield from g()
def f_1(): yield from g()
def f_2(): yield from g()
def f_3(): yield from g()
def f_4(): yield from g()
def f_5(): yield from g()
def f_6(): yield from g()
def f_7(): yield from g()
def f_8(): yield from g()
def f_9(): yield from g()
```

```
$ time pyright po.py
0 errors, 0 warnings, 0 informations

real 25m25.431s
user 25m24.261s
sys 0m3.227s
```

| # of `f_N` | seconds |
|--:|--:|
| 5 | 8 |
| 6 | 29 |
| 7 | 98 |
| 8 | 278 |
| 9 | 711 |
| 10 | 1524 |

if `g` is declared as `def g() -> A[int | str] | B[int | str]: ...`, the problem does not occur.

**VS Code extension or command-line**

command-line tool, 1.1.407

Contributor guide

Open the contributing guide

Research direction

Start by running the command-line tool against the reported po.py-style snippet and compare the type-alias return annotation with the direct union annotation. Trace the analysis of yield from and generic type aliases; done means the alias form no longer shows exponential slowdown while producing the same diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.