Pyright does not respect `__class_getitem__` annotations.
Author: imgurbot12Created Jul 24, 2026Updated Sep 9, 2026
Labelsbug
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
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
Source: microsoft/pyright