Skip to content

Commit 40edd4d

Browse files
committed
fix
1 parent 21755ad commit 40edd4d

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

mypy/checkpattern.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -267,51 +267,53 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
267267
contracted_new_inner_types.append(typ)
268268
contracted_rest_inner_types.append(rest)
269269
self.update_type_map(captures, type_map)
270-
new_inner_types = self.expand_starred_pattern_types(
271-
contracted_new_inner_types, star_position, len(inner_types)
272-
)
273-
rest_inner_types = self.expand_starred_pattern_types(
274-
contracted_rest_inner_types, star_position, len(inner_types)
275-
)
276270

277-
#
278-
# Calculate new type
279-
#
280271
new_type: Type
281272
rest_type: Type = current_type
282-
if not can_match:
283-
new_type = UninhabitedType()
284-
elif isinstance(current_type, TupleType):
285-
narrowed_inner_types = []
286-
inner_rest_types = []
287-
for inner_type, new_inner_type in zip(inner_types, new_inner_types):
288-
(
289-
narrowed_inner_type,
290-
inner_rest_type,
291-
) = self.chk.conditional_types_with_intersection(
292-
new_inner_type, [get_type_range(inner_type)], o, default=new_inner_type
293-
)
294-
narrowed_inner_types.append(narrowed_inner_type)
295-
inner_rest_types.append(inner_rest_type)
296-
if all(not is_uninhabited(typ) for typ in narrowed_inner_types):
297-
new_type = TupleType(narrowed_inner_types, current_type.partial_fallback)
298-
else:
299-
new_type = UninhabitedType()
273+
if can_match:
274+
new_inner_types = self.expand_starred_pattern_types(
275+
contracted_new_inner_types, star_position, len(inner_types)
276+
)
277+
rest_inner_types = self.expand_starred_pattern_types(
278+
contracted_rest_inner_types, star_position, len(inner_types)
279+
)
300280

301-
if all(is_uninhabited(typ) for typ in inner_rest_types):
302-
# All subpatterns always match, so we can apply negative narrowing
303-
rest_type = TupleType(rest_inner_types, current_type.partial_fallback)
304-
else:
305-
new_inner_type = UninhabitedType()
306-
for typ in new_inner_types:
307-
new_inner_type = join_types(new_inner_type, typ)
308-
new_type = self.construct_sequence_child(current_type, new_inner_type)
309-
if is_subtype(new_type, current_type):
310-
new_type, _ = self.chk.conditional_types_with_intersection(
311-
current_type, [get_type_range(new_type)], o, default=current_type
312-
)
281+
#
282+
# Calculate new type
283+
#
284+
if isinstance(current_type, TupleType):
285+
narrowed_inner_types = []
286+
inner_rest_types = []
287+
for inner_type, new_inner_type in zip(inner_types, new_inner_types):
288+
(
289+
narrowed_inner_type,
290+
inner_rest_type,
291+
) = self.chk.conditional_types_with_intersection(
292+
new_inner_type, [get_type_range(inner_type)], o, default=new_inner_type
293+
)
294+
narrowed_inner_types.append(narrowed_inner_type)
295+
inner_rest_types.append(inner_rest_type)
296+
if all(not is_uninhabited(typ) for typ in narrowed_inner_types):
297+
new_type = TupleType(narrowed_inner_types, current_type.partial_fallback)
298+
else:
299+
new_type = UninhabitedType()
300+
301+
if all(is_uninhabited(typ) for typ in inner_rest_types):
302+
# All subpatterns always match, so we can apply negative narrowing
303+
rest_type = TupleType(rest_inner_types, current_type.partial_fallback)
313304
else:
314-
new_type = current_type
305+
new_inner_type = UninhabitedType()
306+
for typ in new_inner_types:
307+
new_inner_type = join_types(new_inner_type, typ)
308+
new_type = self.construct_sequence_child(current_type, new_inner_type)
309+
if is_subtype(new_type, current_type):
310+
new_type, _ = self.chk.conditional_types_with_intersection(
311+
current_type, [get_type_range(new_type)], o, default=current_type
312+
)
313+
else:
314+
new_type = current_type
315+
else:
316+
new_type = UninhabitedType()
315317
return PatternType(new_type, rest_type, captures)
316318

317319
def get_sequence_type(self, t: Type) -> Type | None:

0 commit comments

Comments
 (0)