Skip to content

Commit 34f3387

Browse files
committed
Refactor: replace make_simplified_union with UnionType.make_union in checker.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 ea923b4 commit 34f3387

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

mypy/checker.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,6 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
750750
defn.is_explicit_override
751751
and not found_method_base_classes
752752
and found_method_base_classes is not None
753-
# If the class has Any fallback, we can't be certain that a method
754-
# is really missing - it might come from unfollowed import.
755-
and not defn.info.fallback_to_any
756753
):
757754
self.msg.no_overridable_method(defn.name, defn)
758755
self.check_explicit_override_decorator(defn, found_method_base_classes, defn.impl)
@@ -789,7 +786,7 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
789786
if isinstance(inner_call, FunctionLike):
790787
outer_type = inner_call
791788
elif isinstance(inner_type, UnionType):
792-
union_type = make_simplified_union(inner_type.items)
789+
union_type = UnionType.make_union(inner_type.items)
793790
if isinstance(union_type, UnionType):
794791
items = []
795792
for item in union_type.items:
@@ -1024,7 +1021,7 @@ def get_generator_yield_type(self, return_type: Type, is_coroutine: bool) -> Typ
10241021
if isinstance(return_type, AnyType):
10251022
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
10261023
elif isinstance(return_type, UnionType):
1027-
return make_simplified_union(
1024+
return UnionType.make_union(
10281025
[self.get_generator_yield_type(item, is_coroutine) for item in return_type.items]
10291026
)
10301027
elif not self.is_generator_return_type(
@@ -1058,7 +1055,7 @@ def get_generator_receive_type(self, return_type: Type, is_coroutine: bool) -> T
10581055
if isinstance(return_type, AnyType):
10591056
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
10601057
elif isinstance(return_type, UnionType):
1061-
return make_simplified_union(
1058+
return UnionType.make_union(
10621059
[self.get_generator_receive_type(item, is_coroutine) for item in return_type.items]
10631060
)
10641061
elif not self.is_generator_return_type(
@@ -1101,7 +1098,7 @@ def get_generator_return_type(self, return_type: Type, is_coroutine: bool) -> Ty
11011098
if isinstance(return_type, AnyType):
11021099
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
11031100
elif isinstance(return_type, UnionType):
1104-
return make_simplified_union(
1101+
return UnionType.make_union(
11051102
[self.get_generator_return_type(item, is_coroutine) for item in return_type.items]
11061103
)
11071104
elif not self.is_generator_return_type(return_type, is_coroutine):
@@ -2332,7 +2329,7 @@ def get_op_other_domain(self, tp: FunctionLike) -> Type | None:
23322329
raw_items = [self.get_op_other_domain(it) for it in tp.items]
23332330
items = [it for it in raw_items if it]
23342331
if items:
2335-
return make_simplified_union(items)
2332+
return UnionType.make_union(items)
23362333
return None
23372334
else:
23382335
assert False, "Need to check all FunctionLike subtypes here"
@@ -3184,7 +3181,7 @@ def check_assignment(
31843181
if not self.current_node_deferred:
31853182
# Partial type can't be final, so strip any literal values.
31863183
rvalue_type = remove_instance_last_known_values(rvalue_type)
3187-
inferred_type = make_simplified_union([rvalue_type, NoneType()])
3184+
inferred_type = UnionType.make_union([rvalue_type, NoneType()])
31883185
self.set_inferred_type(var, lvalue, inferred_type)
31893186
else:
31903187
var.type = None
@@ -4000,7 +3997,7 @@ def check_multi_assignment_from_union(
40003997
# We can access _type_maps directly since temporary type maps are
40013998
# only created within expressions.
40023999
t.append(self._type_maps[0].pop(lv, AnyType(TypeOfAny.special_form)))
4003-
union_types = tuple(make_simplified_union(col) for col in transposed)
4000+
union_types = tuple(UnionType.make_union(col) for col in transposed)
40044001
for expr, items in assignments.items():
40054002
# Bind a union of types collected in 'assignments' to every expression.
40064003
if isinstance(expr, StarExpr):
@@ -4016,8 +4013,8 @@ def check_multi_assignment_from_union(
40164013
types, declared_types = zip(*clean_items)
40174014
self.binder.assign_type(
40184015
expr,
4019-
make_simplified_union(list(types)),
4020-
make_simplified_union(list(declared_types)),
4016+
UnionType.make_union(list(types)),
4017+
UnionType.make_union(list(declared_types)),
40214018
)
40224019
for union, lv in zip(union_types, self.flatten_lvalues(lvalues)):
40234020
# Properly store the inferred types.
@@ -4510,7 +4507,7 @@ def check_simple_assignment(
45104507
# at module level or class bodies can't be widened in functions, or
45114508
# in another module.
45124509
if not self.refers_to_different_scope(lvalue):
4513-
lvalue_type = make_simplified_union([inferred.type, new_inferred])
4510+
lvalue_type = UnionType.make_union([inferred.type, new_inferred])
45144511
if not is_same_type(lvalue_type, inferred.type) and not isinstance(
45154512
inferred.type, PartialType
45164513
):
@@ -5064,7 +5061,7 @@ def check_except_handler_test(self, n: Expression, is_star: bool) -> Type:
50645061
else:
50655062
new_all_types.append(typ)
50665063
return self.wrap_exception_group(new_all_types)
5067-
return make_simplified_union(all_types)
5064+
return UnionType.make_union(all_types)
50685065

50695066
def default_exception_type(self, is_star: bool) -> Type:
50705067
"""Exception type to return in case of a previous type error."""
@@ -5075,7 +5072,7 @@ def default_exception_type(self, is_star: bool) -> Type:
50755072

50765073
def wrap_exception_group(self, types: Sequence[Type]) -> Type:
50775074
"""Transform except* variable type into an appropriate exception group."""
5078-
arg = make_simplified_union(types)
5075+
arg = UnionType.make_union(types)
50795076
if is_subtype(arg, self.named_type("builtins.Exception")):
50805077
base = "builtins.ExceptionGroup"
50815078
else:
@@ -5288,15 +5285,12 @@ def visit_decorator_inner(
52885285
# For overloaded functions/properties we already checked override for overload as a whole.
52895286
if allow_empty or skip_first_item:
52905287
return
5291-
if e.func.info and not e.is_overload:
5288+
if e.func.info and not e.func.is_dynamic() and not e.is_overload:
52925289
found_method_base_classes = self.check_method_override(e)
52935290
if (
52945291
e.func.is_explicit_override
52955292
and not found_method_base_classes
52965293
and found_method_base_classes is not None
5297-
# If the class has Any fallback, we can't be certain that a method
5298-
# is really missing - it might come from unfollowed import.
5299-
and not e.func.info.fallback_to_any
53005294
):
53015295
self.msg.no_overridable_method(e.func.name, e.func)
53025296
self.check_explicit_override_decorator(e.func, found_method_base_classes)
@@ -6569,7 +6563,7 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
65696563
member_types = [new_parent_type.items[key] for key in str_literals]
65706564
except KeyError:
65716565
return None
6572-
return make_simplified_union(member_types)
6566+
return UnionType.make_union(member_types)
65736567

65746568
else:
65756569
int_literals = try_getting_int_literals_from_type(index_type)
@@ -6583,7 +6577,7 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
65836577
member_types = [new_parent_type.items[key] for key in int_literals]
65846578
except IndexError:
65856579
return None
6586-
return make_simplified_union(member_types)
6580+
return UnionType.make_union(member_types)
65876581

65886582
else:
65896583
return output
@@ -6623,7 +6617,7 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
66236617
return output
66246618

66256619
expr = parent_expr
6626-
expr_type = output[parent_expr] = make_simplified_union(new_parent_types)
6620+
expr_type = output[parent_expr] = UnionType.make_union(new_parent_types)
66276621

66286622
def refine_identity_comparison_expression(
66296623
self,
@@ -6975,11 +6969,11 @@ def narrow_with_len(self, typ: Type, op: str, size: int) -> tuple[Type | None, T
69756969
yes_types += other_types
69766970
no_types += other_types
69776971
if yes_types:
6978-
yes_type = make_simplified_union(yes_types)
6972+
yes_type = UnionType.make_union(yes_types)
69796973
else:
69806974
yes_type = None
69816975
if no_types:
6982-
no_type = make_simplified_union(no_types)
6976+
no_type = UnionType.make_union(no_types)
69836977
else:
69846978
no_type = None
69856979
return yes_type, no_type
@@ -7653,7 +7647,7 @@ def conditional_types_with_intersection(
76537647
for types, reason in errors:
76547648
self.msg.impossible_intersection(types, reason, ctx)
76557649
return UninhabitedType(), expr_type
7656-
new_yes_type = make_simplified_union(out)
7650+
new_yes_type = UnionType.make_union(out)
76577651
return new_yes_type, expr_type
76587652

76597653
def is_writable_attribute(self, node: Node) -> bool:
@@ -7777,7 +7771,7 @@ def add_any_attribute_to_type(self, typ: Type, name: str) -> Type:
77777771
)
77787772
if isinstance(typ, UnionType):
77797773
with_attr, without_attr = self.partition_union_by_attr(typ, name)
7780-
return make_simplified_union(
7774+
return UnionType.make_union(
77817775
with_attr + [self.add_any_attribute_to_type(typ, name) for typ in without_attr]
77827776
)
77837777
return orig_typ
@@ -7800,7 +7794,7 @@ def hasattr_type_maps(
78007794
if isinstance(source_type, UnionType):
78017795
_, without_attr = self.partition_union_by_attr(source_type, name)
78027796
yes_map = {expr: self.add_any_attribute_to_type(source_type, name)}
7803-
return yes_map, {expr: make_simplified_union(without_attr)}
7797+
return yes_map, {expr: UnionType.make_union(without_attr)}
78047798

78057799
type_with_attr = self.add_any_attribute_to_type(source_type, name)
78067800
if type_with_attr != source_type:
@@ -7945,7 +7939,7 @@ def conditional_types(
79457939
enum_name = target.fallback.type.fullname
79467940
current_type = try_expanding_sum_type_to_union(current_type, enum_name)
79477941
proposed_items = [type_range.item for type_range in proposed_type_ranges]
7948-
proposed_type = make_simplified_union(proposed_items)
7942+
proposed_type = UnionType.make_union(proposed_items)
79497943
if isinstance(proposed_type, AnyType):
79507944
# We don't really know much about the proposed type, so we shouldn't
79517945
# attempt to narrow anything. Instead, we broaden the expr to Any to
@@ -8084,7 +8078,7 @@ def builtin_item_type(tp: Type) -> Type | None:
80848078
else:
80858079
normalized_items.append(it)
80868080
if all(not isinstance(it, AnyType) for it in get_proper_types(normalized_items)):
8087-
return make_simplified_union(normalized_items) # this type is not externally visible
8081+
return UnionType.make_union(normalized_items) # this type is not externally visible
80888082
elif isinstance(tp, TypedDictType):
80898083
# TypedDict always has non-optional string keys. Find the key type from the Mapping
80908084
# base class.
@@ -8147,7 +8141,7 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap, coalesce_any: bool = False) ->
81478141
if coalesce_any and isinstance(get_proper_type(m1[n1]), AnyType):
81488142
result[n1] = m1[n1]
81498143
else:
8150-
result[n1] = make_simplified_union([m1[n1], m2[n2]])
8144+
result[n1] = UnionType.make_union([m1[n1], m2[n2]])
81518145
return result
81528146

81538147

0 commit comments

Comments
 (0)