Skip to content

Commit 010b328

Browse files
committed
Move code of distribute tuple
1 parent 383eb17 commit 010b328

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

mypy/subtypes.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,6 @@ def is_subtype(
189189
return _is_subtype(left, right, subtype_context, proper_subtype=False)
190190
left = get_proper_type(left)
191191
right = get_proper_type(right)
192-
193-
# Special case: distribute Tuple unions before fallback subtype check
194-
if isinstance(left, TupleType) and isinstance(right, UnionType):
195-
items = [get_proper_type(item) for item in left.items]
196-
if any(isinstance(item, UnionType) for item in items):
197-
expanded = []
198-
for item in items:
199-
if isinstance(item, UnionType):
200-
expanded.append(item.items)
201-
else:
202-
expanded.append([item])
203-
distributed = []
204-
for combo in product(*expanded):
205-
fb = left.partial_fallback
206-
if hasattr(left, "fallback") and left.fallback is not None:
207-
fb = left.fallback
208-
distributed.append(TupleType(list(combo), fallback=fb))
209-
simplified = make_simplified_union(distributed)
210-
if is_equivalent(simplified, right):
211-
return True
212-
return _is_subtype(simplified, right, subtype_context, proper_subtype=False)
213192
return _is_subtype(left, right, subtype_context, proper_subtype=False)
214193

215194

@@ -328,7 +307,23 @@ def _is_subtype(
328307
# TODO: should we consider all types proper subtypes of UnboundType and/or
329308
# ErasedType as we do for non-proper subtyping.
330309
return True
331-
310+
if isinstance(left, TupleType) and isinstance(right, UnionType):
311+
items = [get_proper_type(item) for item in left.items]
312+
if any(isinstance(item, UnionType) for item in items):
313+
expanded = []
314+
for item in items:
315+
if isinstance(item, UnionType):
316+
expanded.append(item.items)
317+
else:
318+
expanded.append([item])
319+
distributed = []
320+
for combo in product(*expanded):
321+
fb = left.partial_fallback
322+
if hasattr(left, "fallback") and left.fallback is not None:
323+
fb = left.fallback
324+
distributed.append(TupleType(list(combo), fallback=fb))
325+
simplified = make_simplified_union(distributed)
326+
return _is_subtype(simplified, right, subtype_context, proper_subtype=False)
332327
if isinstance(right, UnionType) and not isinstance(left, UnionType):
333328
# Normally, when 'left' is not itself a union, the only way
334329
# 'left' can be a subtype of the union 'right' is if it is a

0 commit comments

Comments
 (0)