Pyright hangs when a unification variable occurs inside itself
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
## Description
When a unification variable occurs inside itself (for example, in the context of a recursive type), Pyright hangs.
## Code or Screenshots
```python
import typing
def hummingbird[A, B, C](a: typing.Callable[[B, C, B], A]) -> typing.Callable[[B, C], A]:
return lambda b, c: a(b, c, b)
def vireo[A, B, C](a: A, b: B, c: typing.Callable[[A, B], C]) -> C:
return c(a, b)
typing.reveal_type(hummingbird(vireo))
```
I am running `pyright` from the command line.
```sh
$ pyright --verbose repro.py
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Assuming Python version 3.13.2.final.0
Execution environment: python
Extra paths:
(none)
Python version: 3.13.2.final.0
Python platform: Linux
Search paths:
/usr/lib/node_modules/pyright/dist/typeshed-fallback/stdlib
/tmp/pyright-crash-repro
/tmp/pyright-crash-repro/typings
/usr/lib/node_modules/pyright/dist/typeshed-fallback/stubs/...
/usr/lib/python3.13
/usr/lib/python3.13/lib-dynload
/home/clarisse/.local/lib/python3.13/site-packages
/usr/lib/python3.13/site-packages
Found 1 source file
```
Happens with Pyright v1.1.398.
Interestingly, mypy seems to have an escape by giving up and filling with `Never`s.
```
$ mypy repro.py
repro.py:11:20: note: Revealed type is "def (Never, Never) -> Never"
Success: no issues found in 1 source file
```
In pure theory, `x` should have the type `((((?0, ?1) -> ?2) as ?0), ?1) -> ?2`, which is impossible to express in the current type system as far as I am aware.
Contributor guide
Research direction
Save the supplied snippet as repro.py and run `pyright --verbose repro.py` to reproduce the hang. Then trace the type-inference and unification handling for a variable occurring inside itself. Done means this input terminates without hanging and the regression is covered by an appropriate test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100