Skip to content

Refactor: replace make_simplified_union with UnionType.make_union (#8624) #19026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ea923b4
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
34f3387
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
f469a4c
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
50afb03
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
ec2f254
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
4ad5492
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
65e00ff
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
3c29b1d
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
fc16c07
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
4cb17eb
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
34930f8
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
404d839
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
b5218d6
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
cdc64c0
Refactor: replace make_simplified_union with UnionType.make_union in …
lsrafael13 May 4, 2025
1f1bc26
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2025
0f9e25e
style: apply formatting fixes using ruff and black
lsrafael13 May 4, 2025
795baca
Merge branch 'issue-8624-refactor' of https://github.com/lsrafael13/m…
lsrafael13 May 4, 2025
acb71a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2025
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
3 changes: 1 addition & 2 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from mypy.nodes import Expression, IndexExpr, MemberExpr, NameExpr, RefExpr, TypeInfo, Var
from mypy.options import Options
from mypy.subtypes import is_same_type, is_subtype
from mypy.typeops import make_simplified_union
from mypy.types import (
AnyType,
Instance,
Expand Down Expand Up @@ -277,7 +276,7 @@ def update_from_options(self, frames: list[Frame]) -> bool:
# interfere with our (hacky) TypeGuard support.
type = possible_types[0]
else:
type = make_simplified_union(possible_types)
type = UnionType.make_union(possible_types)
# Legacy guard for corner case when the original type is TypeVarType.
if isinstance(declaration_type, TypeVarType) and not is_subtype(
type, declaration_type
Expand Down
55 changes: 23 additions & 32 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
function_type,
is_literal_type_like,
is_singleton_type,
make_simplified_union,
true_only,
try_expanding_sum_type_to_union,
try_getting_int_literals_from_type,
Expand Down Expand Up @@ -750,9 +749,6 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
defn.is_explicit_override
and not found_method_base_classes
and found_method_base_classes is not None
# If the class has Any fallback, we can't be certain that a method
# is really missing - it might come from unfollowed import.
and not defn.info.fallback_to_any
):
self.msg.no_overridable_method(defn.name, defn)
self.check_explicit_override_decorator(defn, found_method_base_classes, defn.impl)
Expand Down Expand Up @@ -789,7 +785,7 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
if isinstance(inner_call, FunctionLike):
outer_type = inner_call
elif isinstance(inner_type, UnionType):
union_type = make_simplified_union(inner_type.items)
union_type = UnionType.make_union(inner_type.items)
if isinstance(union_type, UnionType):
items = []
for item in union_type.items:
Expand Down Expand Up @@ -1024,7 +1020,7 @@ def get_generator_yield_type(self, return_type: Type, is_coroutine: bool) -> Typ
if isinstance(return_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
elif isinstance(return_type, UnionType):
return make_simplified_union(
return UnionType.make_union(
[self.get_generator_yield_type(item, is_coroutine) for item in return_type.items]
)
elif not self.is_generator_return_type(
Expand Down Expand Up @@ -1058,7 +1054,7 @@ def get_generator_receive_type(self, return_type: Type, is_coroutine: bool) -> T
if isinstance(return_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
elif isinstance(return_type, UnionType):
return make_simplified_union(
return UnionType.make_union(
[self.get_generator_receive_type(item, is_coroutine) for item in return_type.items]
)
elif not self.is_generator_return_type(
Expand Down Expand Up @@ -1101,7 +1097,7 @@ def get_generator_return_type(self, return_type: Type, is_coroutine: bool) -> Ty
if isinstance(return_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
elif isinstance(return_type, UnionType):
return make_simplified_union(
return UnionType.make_union(
[self.get_generator_return_type(item, is_coroutine) for item in return_type.items]
)
elif not self.is_generator_return_type(return_type, is_coroutine):
Expand Down Expand Up @@ -2332,7 +2328,7 @@ def get_op_other_domain(self, tp: FunctionLike) -> Type | None:
raw_items = [self.get_op_other_domain(it) for it in tp.items]
items = [it for it in raw_items if it]
if items:
return make_simplified_union(items)
return UnionType.make_union(items)
return None
else:
assert False, "Need to check all FunctionLike subtypes here"
Expand Down Expand Up @@ -3184,7 +3180,7 @@ def check_assignment(
if not self.current_node_deferred:
# Partial type can't be final, so strip any literal values.
rvalue_type = remove_instance_last_known_values(rvalue_type)
inferred_type = make_simplified_union([rvalue_type, NoneType()])
inferred_type = UnionType.make_union([rvalue_type, NoneType()])
self.set_inferred_type(var, lvalue, inferred_type)
else:
var.type = None
Expand Down Expand Up @@ -4000,7 +3996,7 @@ def check_multi_assignment_from_union(
# We can access _type_maps directly since temporary type maps are
# only created within expressions.
t.append(self._type_maps[0].pop(lv, AnyType(TypeOfAny.special_form)))
union_types = tuple(make_simplified_union(col) for col in transposed)
union_types = tuple(UnionType.make_union(col) for col in transposed)
for expr, items in assignments.items():
# Bind a union of types collected in 'assignments' to every expression.
if isinstance(expr, StarExpr):
Expand All @@ -4015,9 +4011,7 @@ def check_multi_assignment_from_union(

types, declared_types = zip(*clean_items)
self.binder.assign_type(
expr,
make_simplified_union(list(types)),
make_simplified_union(list(declared_types)),
expr, UnionType.make_union(list(types)), UnionType.make_union(list(declared_types))
)
for union, lv in zip(union_types, self.flatten_lvalues(lvalues)):
# Properly store the inferred types.
Expand Down Expand Up @@ -4510,7 +4504,7 @@ def check_simple_assignment(
# at module level or class bodies can't be widened in functions, or
# in another module.
if not self.refers_to_different_scope(lvalue):
lvalue_type = make_simplified_union([inferred.type, new_inferred])
lvalue_type = UnionType.make_union([inferred.type, new_inferred])
if not is_same_type(lvalue_type, inferred.type) and not isinstance(
inferred.type, PartialType
):
Expand Down Expand Up @@ -5064,7 +5058,7 @@ def check_except_handler_test(self, n: Expression, is_star: bool) -> Type:
else:
new_all_types.append(typ)
return self.wrap_exception_group(new_all_types)
return make_simplified_union(all_types)
return UnionType.make_union(all_types)

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

def wrap_exception_group(self, types: Sequence[Type]) -> Type:
"""Transform except* variable type into an appropriate exception group."""
arg = make_simplified_union(types)
arg = UnionType.make_union(types)
if is_subtype(arg, self.named_type("builtins.Exception")):
base = "builtins.ExceptionGroup"
else:
Expand Down Expand Up @@ -5288,15 +5282,12 @@ def visit_decorator_inner(
# For overloaded functions/properties we already checked override for overload as a whole.
if allow_empty or skip_first_item:
return
if e.func.info and not e.is_overload:
if e.func.info and not e.func.is_dynamic() and not e.is_overload:
found_method_base_classes = self.check_method_override(e)
if (
e.func.is_explicit_override
and not found_method_base_classes
and found_method_base_classes is not None
# If the class has Any fallback, we can't be certain that a method
# is really missing - it might come from unfollowed import.
and not e.func.info.fallback_to_any
):
self.msg.no_overridable_method(e.func.name, e.func)
self.check_explicit_override_decorator(e.func, found_method_base_classes)
Expand Down Expand Up @@ -6569,7 +6560,7 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
member_types = [new_parent_type.items[key] for key in str_literals]
except KeyError:
return None
return make_simplified_union(member_types)
return UnionType.make_union(member_types)

else:
int_literals = try_getting_int_literals_from_type(index_type)
Expand All @@ -6583,7 +6574,7 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
member_types = [new_parent_type.items[key] for key in int_literals]
except IndexError:
return None
return make_simplified_union(member_types)
return UnionType.make_union(member_types)

else:
return output
Expand Down Expand Up @@ -6623,7 +6614,7 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
return output

expr = parent_expr
expr_type = output[parent_expr] = make_simplified_union(new_parent_types)
expr_type = output[parent_expr] = UnionType.make_union(new_parent_types)

def refine_identity_comparison_expression(
self,
Expand Down Expand Up @@ -6975,11 +6966,11 @@ def narrow_with_len(self, typ: Type, op: str, size: int) -> tuple[Type | None, T
yes_types += other_types
no_types += other_types
if yes_types:
yes_type = make_simplified_union(yes_types)
yes_type = UnionType.make_union(yes_types)
else:
yes_type = None
if no_types:
no_type = make_simplified_union(no_types)
no_type = UnionType.make_union(no_types)
else:
no_type = None
return yes_type, no_type
Expand Down Expand Up @@ -7653,7 +7644,7 @@ def conditional_types_with_intersection(
for types, reason in errors:
self.msg.impossible_intersection(types, reason, ctx)
return UninhabitedType(), expr_type
new_yes_type = make_simplified_union(out)
new_yes_type = UnionType.make_union(out)
return new_yes_type, expr_type

def is_writable_attribute(self, node: Node) -> bool:
Expand Down Expand Up @@ -7777,7 +7768,7 @@ def add_any_attribute_to_type(self, typ: Type, name: str) -> Type:
)
if isinstance(typ, UnionType):
with_attr, without_attr = self.partition_union_by_attr(typ, name)
return make_simplified_union(
return UnionType.make_union(
with_attr + [self.add_any_attribute_to_type(typ, name) for typ in without_attr]
)
return orig_typ
Expand All @@ -7800,7 +7791,7 @@ def hasattr_type_maps(
if isinstance(source_type, UnionType):
_, without_attr = self.partition_union_by_attr(source_type, name)
yes_map = {expr: self.add_any_attribute_to_type(source_type, name)}
return yes_map, {expr: make_simplified_union(without_attr)}
return yes_map, {expr: UnionType.make_union(without_attr)}

type_with_attr = self.add_any_attribute_to_type(source_type, name)
if type_with_attr != source_type:
Expand Down Expand Up @@ -7945,7 +7936,7 @@ def conditional_types(
enum_name = target.fallback.type.fullname
current_type = try_expanding_sum_type_to_union(current_type, enum_name)
proposed_items = [type_range.item for type_range in proposed_type_ranges]
proposed_type = make_simplified_union(proposed_items)
proposed_type = UnionType.make_union(proposed_items)
if isinstance(proposed_type, AnyType):
# We don't really know much about the proposed type, so we shouldn't
# attempt to narrow anything. Instead, we broaden the expr to Any to
Expand Down Expand Up @@ -8084,7 +8075,7 @@ def builtin_item_type(tp: Type) -> Type | None:
else:
normalized_items.append(it)
if all(not isinstance(it, AnyType) for it in get_proper_types(normalized_items)):
return make_simplified_union(normalized_items) # this type is not externally visible
return UnionType.make_union(normalized_items) # this type is not externally visible
elif isinstance(tp, TypedDictType):
# TypedDict always has non-optional string keys. Find the key type from the Mapping
# base class.
Expand Down Expand Up @@ -8147,7 +8138,7 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap, coalesce_any: bool = False) ->
if coalesce_any and isinstance(get_proper_type(m1[n1]), AnyType):
result[n1] = m1[n1]
else:
result[n1] = make_simplified_union([m1[n1], m2[n2]])
result[n1] = UnionType.make_union([m1[n1], m2[n2]])
return result


Expand Down
Loading
Loading