Skip to content

Commit ac56c58

Browse files
committed
okay try this
1 parent 40edd4d commit ac56c58

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

mypy/checkpattern.py

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -257,63 +257,58 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
257257
contracted_inner_types = self.contract_starred_pattern_types(
258258
inner_types, star_position, required_patterns
259259
)
260-
can_match = True
261260
for p, t in zip(o.patterns, contracted_inner_types):
262261
pattern_type = self.accept(p, t)
263262
typ, rest, type_map = pattern_type
264-
if is_uninhabited(typ):
265-
can_match = False
266-
else:
267-
contracted_new_inner_types.append(typ)
268-
contracted_rest_inner_types.append(rest)
263+
contracted_new_inner_types.append(typ)
264+
contracted_rest_inner_types.append(rest)
269265
self.update_type_map(captures, type_map)
270266

271267
new_type: Type
272268
rest_type: Type = current_type
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-
)
280269

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()
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+
)
300276

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

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

test-data/unit/check-python310.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ SubClass: type[Example]
327327

328328
match [SubClass("a"), SubClass("b")]:
329329
case [SubClass(value), *rest]: # E: Expected type in class pattern; found "Type[__main__.Example]"
330-
pass
330+
reveal_type(value) # E: Cannot determine type of "value" \
331+
# N: Revealed type is "Any"
332+
reveal_type(rest) # N: Revealed type is "builtins.list[__main__.Example]"
331333
[builtins fixtures/tuple.pyi]
332334

333335
[case testMatchSequenceUnion-skip]

0 commit comments

Comments
 (0)