#4542·ty

Collection literal inference can narrow to an element that produces diagnostics

Author: ibraheemdevCreated Sep 17, 2026Updated Sep 17, 2026
Labelsbugbidirectional inference

When a generic calll is inferred against a union type context, we narrow to the first union element to which the inferred type is assignable. However, an inference attempt that produces diagnostics may be considered assignable due to a fallback type of Unknown, despite another union element being more compatible.

python
from typing import Callable

def f[T](x: T) -> list[T]:
    return [x]

f: list[Callable[[int], int]] | list[Callable[[str], str]] = f(lambda x: x.upper())

This can also happen during collection literal inference:

python
f: list[Callable[[int], int]] | list[Callable[[str], str]] = [
    lambda x: x.upper(),  # error: [unresolved-attribute]
]

Related: https://github.com/astral-sh/ty/issues/4311