microsoft / microsoft/pyright

Matching generic return type of `SupportsGetItem`

Open
#11,544 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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 match the generic return type of `SupportsGetItem` properly.

# Code or Screenshots

```python
from __future__ import annotations

from typing import TYPE_CHECKING, Any, TypeVar

if TYPE_CHECKING:
from _typeshed import SupportsGetItem

class Series: ...

class DataFrame:
def __getitem__(self, key: object) -> Series:
return Series()

T = TypeVar("T")

def f(x: SupportsGetItem[Any, T]) -> T:
if isinstance(x, DataFrame):
return Series()
else:
return x[0]

```

# VS Code extension or command-line

```text
Type "Series" is not assignable to return type "T@f"
Type "Series" is not assignable to type "T@f" (reportReturnType)
```

- `mypy` works fine: https://mypy-play.net/?gist=3aaea716bd3429a90a96457884f46712
- `pyright` reports an error: https://pyright-play.net/?strict=true&code=GYJw9gtgBA%2BjwFcAuCQFM5QJYQA5hCSgEMA7UsJYpLMUgZwFgAoF0SKJAT1y1IHNseAkQAqATQAKAURgBhABLS5AaQCSAOQDiAGigBBUlz2ieaAGrEQLFlmBQJM%2BUtWatALhZRvUdtBjcuGj0ABZoACZC%2BIRQAMoIuNFI9FpoSGpIaBA2zADGADbE9PRxaCBYwe5QAHS1OQVFJQAi1MQAYiDEEGiezD5Q4Wj2cPxpWJkQcAAU9Gj5wHoA1mhcVWAARgBWaLlIAJRQALQAfKXllV79PugoIKRnFfRTezmiUAC8DmaWIFMARKI-i9WMxBvZgFMAB5VeKJEQpNIZLIAbUMxgcAF0DicHL1%2BnZsPQ%2BPQqKRcmgoXoWlQOl00Hs8VdvDdUPdYmVHs9Lt45rNGUyWXcoJDkQAGDEsIA

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python example in strict mode and comparing pyright's diagnostic with the linked mypy result. Trace how generic return types are inferred for SupportsGetItem and the DataFrame branch. Done means pyright accepts the example without incorrectly reporting that Series is incompatible with T.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.