exponential slowdown when using `yield from` with type alias
Author: pinteriorCreated Oct 28, 2025Updated Sep 22, 2026
Labelsbug
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
#!/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
Source: microsoft/pyright