Skip to content

Commit 0a12cfd

Browse files
committed
support recursive flattening
1 parent b3aafa7 commit 0a12cfd

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
PartialType, DeletedType, UninhabitedType, TypeType, TypeOfAny, LiteralType, LiteralValue,
2020
is_named_instance, FunctionLike,
2121
StarType, is_optional, remove_optional, is_generic_instance, get_proper_type, ProperType,
22-
get_proper_types, flatten_nested_unions, TypeAliasType
22+
get_proper_types, flatten_nested_unions
2323
)
2424
from mypy.nodes import (
2525
NameExpr, RefExpr, Var, FuncDef, OverloadedFuncDef, TypeInfo, CallExpr,
@@ -2521,10 +2521,9 @@ def check_op(self, method: str, base_type: Type,
25212521
left_variants = [base_type]
25222522
base_type = get_proper_type(base_type)
25232523
if isinstance(base_type, UnionType):
2524-
items = [get_proper_type(item) if isinstance(item, TypeAliasType)
2525-
else item for item in base_type.relevant_items()]
25262524
left_variants = [item for item in
2527-
flatten_nested_unions(items)]
2525+
flatten_nested_unions(base_type.relevant_items(),
2526+
handle_type_alias_type=True)]
25282527
right_type = self.accept(arg)
25292528

25302529
# Step 1: We first try leaving the right arguments alone and destructure
@@ -2566,11 +2565,9 @@ def check_op(self, method: str, base_type: Type,
25662565
right_variants = [(right_type, arg)]
25672566
right_type = get_proper_type(right_type)
25682567
if isinstance(right_type, UnionType):
2569-
items = [get_proper_type(item) if isinstance(item, TypeAliasType)
2570-
else item for item in right_type.relevant_items()]
25712568
right_variants = [(item, TempNode(item, context=context))
2572-
for item in flatten_nested_unions(items)]
2573-
2569+
for item in flatten_nested_unions(right_type.relevant_items(),
2570+
handle_type_alias_type=True)]
25742571
msg = self.msg.clean_copy()
25752572
msg.disable_count = 0
25762573
all_results = []

mypy/types.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,15 +2231,20 @@ def has_type_vars(typ: Type) -> bool:
22312231
return typ.accept(HasTypeVars())
22322232

22332233

2234-
def flatten_nested_unions(types: Iterable[Type]) -> List[Type]:
2234+
def flatten_nested_unions(types: Iterable[Type],
2235+
handle_type_alias_type: bool = False) -> List[Type]:
22352236
"""Flatten nested unions in a type list."""
22362237
# This and similar functions on unions can cause infinite recursion
22372238
# if passed a "pathological" alias like A = Union[int, A] or similar.
22382239
# TODO: ban such aliases in semantic analyzer.
22392240
flat_items = [] # type: List[Type]
2241+
if handle_type_alias_type:
2242+
types = [get_proper_type(item) if isinstance(item, TypeAliasType)
2243+
else item for item in types]
22402244
for tp in types:
22412245
if isinstance(tp, ProperType) and isinstance(tp, UnionType):
2242-
flat_items.extend(flatten_nested_unions(tp.items))
2246+
flat_items.extend(flatten_nested_unions(tp.items,
2247+
handle_type_alias_type=handle_type_alias_type))
22432248
else:
22442249
flat_items.append(tp)
22452250
return flat_items

0 commit comments

Comments
 (0)