Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: use union bound as return type for TypeVar with union bound in t…
…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
  • Loading branch information
Bahtya committed Apr 9, 2026
commit 8f458ba0a90435301393b008821657fa5d6e5cad
13 changes: 10 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,14 +1941,21 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
# with typevar.
callee = self.analyze_type_type_callee(get_proper_type(item.upper_bound), context)
callee = get_proper_type(callee)
# When the TypeVar has a union bound, use the bound as the return type
# instead of the TypeVar itself, since calling the constructor should
# return an instance of one of the union members.
return_type = item
upper_bound = get_proper_type(item.upper_bound)
if isinstance(upper_bound, UnionType):
return_type = upper_bound
if isinstance(callee, CallableType):
callee = callee.copy_modified(ret_type=item)
callee = callee.copy_modified(ret_type=return_type)
elif isinstance(callee, Overloaded):
callee = Overloaded([c.copy_modified(ret_type=item) for c in callee.items])
callee = Overloaded([c.copy_modified(ret_type=return_type) for c in callee.items])
elif isinstance(callee, UnionType):
callee = UnionType(
[
self._replace_callable_return_type(tp, item)
self._replace_callable_return_type(tp, return_type)
for tp in callee.relevant_items()
],
callee.line,
Expand Down
Loading