Skip to content

Problem inferring nested generic classes with defaults #11187

@cmp0xff

Description

@cmp0xff

Describe the bug

pyright cannot infer nested generic classes with default values properly.

Code or Screenshots

from typing import Any, Generic
from collections.abc import Sequence
from typing_extensions import TypeVar, Self, reveal_type

_OrderableT = TypeVar("_OrderableT", default=Any)

class Interval(Generic[_OrderableT]):
    def __new__(cls, left: _OrderableT, right: _OrderableT) -> Self: ...

IntervalT = TypeVar("IntervalT", bound=Interval, default=Interval)

S2 = TypeVar("S2")

class IntervalIndex(Generic[S2]):
    def __new__(cls, data: Sequence[IntervalT]) -> IntervalIndex[IntervalT]: ...

reveal_type(Interval(0, 1))  # type checkers all give Interval[int]
reveal_type(IntervalIndex([Interval(0, 1)]))  # mypy gives IntervalIndex[Interval[int]], pyright gives IntervalIndex[Interval[Any]]

VS Code extension or command-line

Command line, pyright==1.1.407

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions