fix: handle union-bound TypeVar in type[T] callable analysis by Bahtya · Pull Request #21191 · python/mypy

@Bahtya

When a TypeVar T has a union bound (e.g. T: bool|int|float|str),
calling a value of type type[T] would incorrectly infer the return
type as the union rather than T. This happened because
analyze_type_type_callee only replaced return types for CallableType
and Overloaded, not for UnionType.

Additionally, CallableType.type_object() would crash with an assertion
error when encountering a TypeVarType whose upper_bound is a union,
since it expected all paths to resolve to an Instance.

Fixes python#21106

Signed-off-by: bahtya <bahtyar153@qq.com>
…ype[T] analysis

When a TypeVar has a union bound (e.g., T = TypeVar("T", bound=A | B)),
analyzing type[T] as a callable should return the union bound (A | B)
as the return type, not the TypeVar (T) itself.

This fixes the testMatchTypeObjectTypeVar test case where calling a
type[T_Choice] where T_Choice is bound to One | Two should return
One | Two, not T_Choice.

Bahtya

@hauntsaninja added the pending

Issues that may be closed

label

Apr 10, 2026