Skip to content

Commit fdab830

Browse files
committed
Preserve type identity
1 parent 412f63b commit fdab830

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

mypy/checkexpr.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,17 +4195,10 @@ def check_op(
41954195
# We don't do the same for the base expression because it could lead to weird
41964196
# type inference errors -- e.g. see 'testOperatorDoubleUnionSum'.
41974197
# TODO: Can we use `type_overrides_set()` here?
4198-
right_variants: list[tuple[Type, Expression]]
4199-
if isinstance(right_type, ProperType) and isinstance(
4200-
right_type, (UnionType, TypeVarType)
4201-
):
4202-
right_variants = [
4203-
(item, TempNode(item, context=context))
4204-
for item in self._union_items_from_typevar(right_type)
4205-
]
4206-
else:
4207-
# Preserve argument identity if we do not intend to modify it
4208-
right_variants = [(right_type, arg)]
4198+
right_variants = [
4199+
(item, TempNode(item, context=context))
4200+
for item in self._union_items_from_typevar(right_type)
4201+
]
42094202
right_type = get_proper_type(right_type)
42104203

42114204
all_results = []
@@ -4262,9 +4255,10 @@ def _union_items_from_typevar(self, typ: Type) -> list[Type]:
42624255
base_type = typ
42634256
if unwrapped := (isinstance(typ, TypeVarType) and not typ.values):
42644257
typ = get_proper_type(typ.upper_bound)
4265-
if isinstance(typ, UnionType):
4258+
if is_union := isinstance(typ, UnionType):
42664259
variants = list(flatten_nested_unions(typ.relevant_items()))
4267-
if unwrapped:
4260+
if is_union and unwrapped:
4261+
# If not a union, keep the original type
42684262
assert isinstance(base_type, TypeVarType)
42694263
variants = [base_type.copy_modified(upper_bound=item) for item in variants]
42704264
return variants

0 commit comments

Comments
 (0)