ty icon indicating copy to clipboard operation
ty copied to clipboard

Problem inferring nested generic classes with defaults

Open cmp0xff opened this issue 3 weeks ago • 1 comments

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

cmp0xff avatar Dec 23 '25 09:12 cmp0xff

Hmm, I hoped this might be fixed by https://github.com/astral-sh/ruff/pull/21930, but it doesn't seem like it.

AlexWaygood avatar Dec 23 '25 11:12 AlexWaygood