Bounded callable return accepts an unconstrained type variable alongside a recursive alias
Author: carljmCreated Sep 30, 2026Updated Sep 30, 2026
Labelsbuggenericsunsoundness
A callable whose return type contains an unconstrained outer type variable can be passed where its return type is bounded by tuple[str, object]. This lets code rely on the first element being a string even when it is an integer.
from typing import Callable
type Recursive = int | list[Recursive]
def forward[R: tuple[str, object]](factory: Callable[[], R]) -> R:
result = factory()
result[0].upper()
return result
def outer[S](value: S) -> None:
def factory() -> tuple[S, Recursive]:
return value, [1]
forward(factory)
outer(1)ty check --python-version 3.12 repro.py reports All checks passed!, but running the example raises AttributeError: 'int' object has no attribute 'upper'. The call to forward should be rejected because S is not bounded by str.
A similar example without a recursive alias is also accepted on this main commit.
Tested with ty built from Ruff main commit ccaed68b562c2c2eeed78395f34be1828c62b885.
Source: astral-sh/ty