microsoft / pyright

Static Type Checker for Python
Other
13.04k stars 1.39k forks source link

Incorrect type evaluation of constructor call via generic type alias #8337

Closed erictraut closed 1 month ago

erictraut commented 1 month ago

If an old-style (pre-PEP 695) generic type alias refers to a generic class, a direct call of this type alias will invoke the constructor for this class. Pyright evaluates the resulting type incorrectly. By contrast, mypy gets this correct.

from typing import Generic, TypeVar

T = TypeVar("T")

class ClassA(Generic[T]):
    def __new__(cls, value: T) -> "ClassA[T]":
        ...

TypeAliasA = ClassA[T]

a2 = TypeAliasA(3)  # Should evaluate to `ClassA[Unknown]`, not `ClassA[int]`
erictraut commented 1 month ago

This is addressed in pyright 1.1.371.