Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 4 additions & 9 deletions mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,13 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
contracted_inner_types = self.contract_starred_pattern_types(
inner_types, star_position, required_patterns
)
can_match = True
for p, t in zip(o.patterns, contracted_inner_types):
pattern_type = self.accept(p, t)
typ, rest, type_map = pattern_type
if is_uninhabited(typ):
can_match = False
else:
contracted_new_inner_types.append(typ)
contracted_rest_inner_types.append(rest)
contracted_new_inner_types.append(typ)
contracted_rest_inner_types.append(rest)
self.update_type_map(captures, type_map)

new_inner_types = self.expand_starred_pattern_types(
contracted_new_inner_types, star_position, len(inner_types)
)
Expand All @@ -279,9 +276,7 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
#
new_type: Type
rest_type: Type = current_type
if not can_match:
new_type = UninhabitedType()
elif isinstance(current_type, TupleType):
if isinstance(current_type, TupleType):
narrowed_inner_types = []
inner_rest_types = []
for inner_type, new_inner_type in zip(inner_types, new_inner_types):
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ match x:
case [str()]:
pass

[case testMatchSequencePatternWithInvalidClassPattern]
class Example:
__match_args__ = ("value",)
def __init__(self, value: str) -> None:
self.value = value

SubClass: type[Example]

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

[case testMatchSequenceUnion-skip]
from typing import List, Union
m: Union[List[List[str]], str]
Expand Down