ty
ty copied to clipboard
Problem inferring nested generic classes with defaults
Summary
ty cannot infer nested generic classes with default values properly.
https://play.ty.dev/4ec45154-7726-41f8-b65c-920188c3c124
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:
return cls()
IntervalT = TypeVar("IntervalT", bound=Interval, default=Interval)
S2 = TypeVar("S2")
class IntervalIndex(Generic[S2]):
def __new__(cls, data: Sequence[IntervalT]) -> "IntervalIndex[IntervalT]":
return cls()
reveal_type(Interval(0, 1)) # type checkers all give Interval[int]
reveal_type(IntervalIndex([Interval(0, 1)])) # mypy gives IntervalIndex[Interval[int]], ty gives IntervalIndex[Unknown]
Version
5ea30c4c5
Hmm, I hoped this might be fixed by https://github.com/astral-sh/ruff/pull/21930, but it doesn't seem like it.