Use Iterable type context when inferring types inside a literal tuple
Summary
This has some similarities with https://github.com/astral-sh/ty/issues/1576 at first glance.
It fails in all those circumstances:
-
floatis replaced by any union type (floatitself is transformed toint | floatas per the error message) -
Iterableis replaced bySequence(and maybe othercollections.abctypes?) - input value is replaced by
setorlist -
dataclassis replaced by either a normal generic class or a generic pydantic model
It does work if we replace Iterable by a concrete container class such as tuple.
It also works if replacing the generic data class by a generic built-in type such as list[T].
Thanks for the tremendous work by the way!
Version
ty 0.0.3 (06305f3c0 2025-12-18)
Thank you for reporting this.
This seems like a problem that is related to invariance and the float special case.
The error goes away if I change v: T to _v: T to make Value covariant in T.
Maybe it's also related to bidirectional type-checking (involving generic protocols), because this works just fine:
def accepts_single_value(value: Value[float]): ...
accepts_single_value(Value(1.))
Right 👍 thanks for the replay, makes sense. Using Final[T] works as well, though specifying the data class as frozen doesn't seem to make it infer the proper variance.