Bounded callable return accepts a caller-fixed type variable alongside an inferred variable
Author: carljmCreated Sep 30, 2026Updated Sep 30, 2026
Labelsbuggenericsunsoundness
A generic callable whose return type contains both an unconstrained outer type variable and an inferred 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
def forward[U, R: tuple[str, object]](factory: Callable[[U], R], arg: U) -> R:
result = factory(arg)
result[0].upper()
return result
def outer[S](value: S) -> None:
def factory[U](arg: U) -> tuple[S, U]:
return value, arg
forward(factory, 1)
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, even though U can be inferred from the argument.
Tested with ty built from Ruff main commit ccaed68b562c2c2eeed78395f34be1828c62b885.
Source: astral-sh/ty