Skip to content

Commit f469a4c

Browse files
committed
Refactor: replace make_simplified_union with UnionType.make_union in checkexpr.py (#8624)
This commit replaces all calls to make_simplified_union with UnionType.make_union in this file, as part of the codebase modernization effort in issue #8624.
1 parent 34f3387 commit f469a4c

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

mypy/checkexpr.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
631631
self.check_str_format_call(e)
632632
ret_type = get_proper_type(ret_type)
633633
if isinstance(ret_type, UnionType):
634-
ret_type = make_simplified_union(ret_type.items)
634+
ret_type = UnionType.make_union(ret_type.items)
635635
if isinstance(ret_type, UninhabitedType) and not ret_type.ambiguous:
636636
self.chk.binder.unreachable()
637637
# Warn on calls to functions that always return None. The check
@@ -1529,7 +1529,7 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
15291529
res.append(
15301530
self.check_call_expr_with_callee_type(narrowed, e, callable_name, item_object_type)
15311531
)
1532-
return make_simplified_union(res)
1532+
return UnionType.make_union(res)
15331533

15341534
def check_call(
15351535
self,
@@ -3205,12 +3205,12 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call
32053205
union_type_guard = None
32063206
union_type_is = None
32073207
else:
3208-
union_type_guard = make_simplified_union(new_type_guards) if new_type_guards else None
3208+
union_type_guard = UnionType.make_union(new_type_guards) if new_type_guards else None
32093209
union_type_is = (
3210-
make_simplified_union(new_type_narrowers) if new_type_narrowers else None
3210+
UnionType.make_union(new_type_narrowers) if new_type_narrowers else None
32113211
)
32123212

3213-
union_return = make_simplified_union(new_returns)
3213+
union_return = UnionType.make_union(new_returns)
32143214

32153215
if too_complex:
32163216
any = AnyType(TypeOfAny.special_form)
@@ -3227,7 +3227,7 @@ def combine_function_signatures(self, types: list[ProperType]) -> AnyType | Call
32273227

32283228
final_args = []
32293229
for args_list in new_args:
3230-
new_type = make_simplified_union(args_list)
3230+
new_type = UnionType.make_union(args_list)
32313231
final_args.append(new_type)
32323232

32333233
return callables[0].copy_modified(
@@ -3334,7 +3334,7 @@ def check_union_call(
33343334
for subtype in callee.relevant_items()
33353335
]
33363336

3337-
return (make_simplified_union([res[0] for res in results]), callee)
3337+
return (UnionType.make_union([res[0] for res in results]), callee)
33383338

33393339
def visit_member_expr(self, e: MemberExpr, is_lvalue: bool = False) -> Type:
33403340
"""Visit member expression (of form e.id)."""
@@ -3927,7 +3927,7 @@ def check_union_method_call_by_name(
39273927
)
39283928
res.append(item)
39293929
meth_res.append(meth_item)
3930-
return make_simplified_union(res), make_simplified_union(meth_res)
3930+
return UnionType.make_union(res), UnionType.make_union(meth_res)
39313931

39323932
def check_method_call(
39333933
self,
@@ -4096,7 +4096,7 @@ def lookup_definer(typ: Instance, attr_name: str) -> str | None:
40964096
results = []
40974097
for name, method, obj, arg in variants:
40984098
with self.msg.filter_errors(save_filtered_errors=True) as local_errors:
4099-
result = self.check_method_call(name, obj, method, [arg], [ARG_POS], context)
4099+
result = self.check_method_call(op_name, obj, method, [arg], [ARG_POS], context)
41004100
if local_errors.has_new_errors():
41014101
errors.append(local_errors.filtered_errors())
41024102
results.append(result)
@@ -4194,8 +4194,8 @@ def check_op(
41944194
all_inferred.append(inferred)
41954195

41964196
if not local_errors.has_new_errors():
4197-
results_final = make_simplified_union(all_results)
4198-
inferred_final = make_simplified_union(all_inferred)
4197+
results_final = UnionType.make_union(all_results)
4198+
inferred_final = UnionType.make_union(all_inferred)
41994199
return results_final, inferred_final
42004200

42014201
# Step 2: If that fails, we try again but also destructure the right argument.
@@ -4253,7 +4253,7 @@ def check_op(
42534253
# See the comment in 'check_overload_call' for more details on why
42544254
# we call 'combine_function_signature' instead of just unioning the inferred
42554255
# callable types.
4256-
results_final = make_simplified_union(all_results)
4256+
results_final = UnionType.make_union(all_results)
42574257
inferred_final = self.combine_function_signatures(get_proper_types(all_inferred))
42584258
return results_final, inferred_final
42594259
else:
@@ -4338,7 +4338,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
43384338
# The left operand is always the result
43394339
return left_type
43404340
else:
4341-
return make_simplified_union([restricted_left_type, right_type])
4341+
return UnionType.make_union([restricted_left_type, right_type])
43424342

43434343
def check_list_multiply(self, e: OpExpr) -> Type:
43444344
"""Type check an expression of form '[...] * e'.
@@ -4451,7 +4451,7 @@ def visit_index_with_type(
44514451
min_len = self.min_tuple_length(left_type)
44524452
self.chk.note(f"Variadic tuple can have length {min_len}", e)
44534453
return AnyType(TypeOfAny.from_error)
4454-
return make_simplified_union(out)
4454+
return UnionType.make_union(out)
44554455
else:
44564456
return self.nonliteral_tuple_index_helper(left_type, index)
44574457
elif isinstance(left_type, TypedDictType):
@@ -4567,7 +4567,7 @@ def visit_tuple_slice_helper(self, left_type: TupleType, slic: SliceExpr) -> Typ
45674567
self.chk.fail(message_registry.AMBIGUOUS_SLICE_OF_VARIADIC_TUPLE, slic)
45684568
return AnyType(TypeOfAny.from_error)
45694569
items.append(item)
4570-
return make_simplified_union(items)
4570+
return UnionType.make_union(items)
45714571

45724572
def try_getting_int_literals(self, index: Expression) -> list[int] | None:
45734573
"""If the given expression or type corresponds to an int literal
@@ -4632,7 +4632,7 @@ def union_tuple_fallback_item(self, left_type: TupleType) -> Type:
46324632
raise NotImplementedError
46334633
else:
46344634
items.append(item)
4635-
return make_simplified_union(items)
4635+
return UnionType.make_union(items)
46364636

46374637
def visit_typeddict_index_expr(
46384638
self, td_type: TypedDictType, index: Expression, setitem: bool = False
@@ -4669,7 +4669,7 @@ def visit_typeddict_index_expr(
46694669
return AnyType(TypeOfAny.from_error), set()
46704670
else:
46714671
value_types.append(value_type)
4672-
return make_simplified_union(value_types), set(key_names)
4672+
return UnionType.make_union(value_types), set(key_names)
46734673

46744674
def visit_enum_index_expr(
46754675
self, enum_type: TypeInfo, index: Expression, context: Context
@@ -5899,7 +5899,7 @@ def visit_conditional_expr(self, e: ConditionalExpr, allow_none_return: bool = F
58995899
if is_literal_type_like(full_context_else_type) and not is_literal_type_like(else_type):
59005900
else_type = full_context_else_type
59015901

5902-
res: Type = make_simplified_union([if_type, else_type])
5902+
res: Type = UnionType.make_union([if_type, else_type])
59035903
if has_uninhabited_component(res) and not isinstance(
59045904
get_proper_type(self.type_context[-1]), UnionType
59055905
):
@@ -6297,13 +6297,7 @@ def narrow_type_from_binder(
62976297
known_type, restriction, prohibit_none_typevar_overlap=True
62986298
):
62996299
return None
6300-
narrowed = narrow_declared_type(known_type, restriction)
6301-
if isinstance(get_proper_type(narrowed), UninhabitedType):
6302-
# If we hit this case, it means that we can't reliably mark the code as
6303-
# unreachable, but the resulting type can't be expressed in type system.
6304-
# Falling back to restriction is more intuitive in most cases.
6305-
return restriction
6306-
return narrowed
6300+
return narrow_declared_type(known_type, restriction)
63076301
return known_type
63086302

63096303
def has_abstract_type_part(self, caller_type: ProperType, callee_type: ProperType) -> bool:

0 commit comments

Comments
 (0)