microsoft / microsoft/pyright

Problem inferring type variables with a default in `Protocol`

Open
#11,179 0 comments 1 reaction 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` does not infer the type variable with a default properly when it comes to a `Protocol`.

## Code or Screenshots

`ttest.py`:

```python
from typing import Any, Generic, Protocol
from typing_extensions import Self, reveal_type, TypeVar

T_co = TypeVar("T_co", covariant=True)
S1 = TypeVar("S1", default=Any)

class SupportRou(Protocol[T_co]):
def __rou__(self) -> T_co: ...

def rou(obj: SupportRou[T_co]) -> T_co:
return obj.__rou__()

class Series(Generic[S1]):
def __rou__(self) -> Self:
return self

reveal_type(rou(Series())) # pyright gives Series[Unknown], mypy gives Series[Any]

reveal_type(rou(Series[int]())) # pyright and mypy both give Series[int]
```

## VS Code extension or command-line

```powershell
poetry run pyright ttest.py
```

I have pyright 1.1.407, python 3.10.11 or 3.14.2

Contributor guide

Open the contributing guide

Research direction

Start with the provided ttest.py reproduction and run `poetry run pyright ttest.py`, comparing its reveal_type output with mypy's. Trace the type-variable inference for Protocol and the defaulted Series type variable; done means `rou(Series())` reveals `Series[Any]` while the explicitly parameterized case remains `Series[int]`.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.