microsoft / microsoft/pyright

Problem inferring nested generic classes with defaults

Open
#11,187 1 comment 2 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
`pyright` cannot infer nested generic classes with default values properly.

## Code or Screenshots

```python
from typing import Any, Generic
from collections.abc import Sequence
from typing_extensions import TypeVar, Self, reveal_type

_OrderableT = TypeVar("_OrderableT", default=Any)

class Interval(Generic[_OrderableT]):
def __new__(cls, left: _OrderableT, right: _OrderableT) -> Self: ...

IntervalT = TypeVar("IntervalT", bound=Interval, default=Interval)

S2 = TypeVar("S2")

class IntervalIndex(Generic[S2]):
def __new__(cls, data: Sequence[IntervalT]) -> IntervalIndex[IntervalT]: ...

reveal_type(Interval(0, 1)) # type checkers all give Interval[int]
reveal_type(IntervalIndex([Interval(0, 1)])) # mypy gives IntervalIndex[Interval[int]], pyright gives IntervalIndex[Interval[Any]]
```

## VS Code extension or command-line

Command line, `pyright==1.1.407`

Contributor guide

Open the contributing guide

Research direction

Reproduce the command-line example with pyright 1.1.407 and inspect the two reveal_type results. Compare the nested result with the expected IntervalIndex[Interval[int]] behavior shown in the issue; the work is done when inference no longer produces Interval[Any] for the nested generic default.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.