microsoft / microsoft/pyright

Pyright does not respect `__class_getitem__` annotations.

Open
#11,561 3 comments 0 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 does not seem to respect type-annotations defined by custom `__class_getitem__` implementations.

It assumes the getitem will return the associated class type regardless of the annotations present.

**Code or Screenshots**

```python
from typing import Annotated, TypeVar, cast, reveal_type

T = TypeVar('T')

class Foo:
@classmethod
def __class_getitem__(cls, type: T) -> T:
return cast(T, Annotated[type, cls()])

foo = Foo[str]
reveal_type(foo) # Type of "foo" is "type[Foo]"
```

In the example above the type should report as `str`.

**VS Code extension or command-line**

This issue exists both in LSP and with the latest version of the pyright command-line `v1.1.411`

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example from the issue with the Pyright command-line tool and confirm whether the same result appears through LSP. Trace handling of custom __class_getitem__ annotations and compare the inferred type with the expected str result. Done means both command-line and LSP report the annotated return type rather than type[Foo].

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
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.