Skip to content

Commit 60b2b5b

Browse files
committed
Handle edge case
1 parent 244b1e4 commit 60b2b5b

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

mypy/checkpattern.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
344344
contracted_rest_inner_types.append(rest)
345345
self.update_type_map(captures, type_map)
346346
if s:
347-
self.update_type_map(captures, {subject: typ for subject in s})
347+
self.update_type_map(
348+
captures, {subject: typ for subject in s}, fail_multiple_assignments=False
349+
)
348350

349351
new_inner_types = self.expand_starred_pattern_types(
350352
contracted_new_inner_types, star_position, len(inner_types), unpack_index is not None
@@ -803,7 +805,10 @@ def generate_types_from_names(self, type_names: list[str]) -> list[Type]:
803805
return types
804806

805807
def update_type_map(
806-
self, original_type_map: dict[Expression, Type], extra_type_map: dict[Expression, Type]
808+
self,
809+
original_type_map: dict[Expression, Type],
810+
extra_type_map: dict[Expression, Type],
811+
fail_multiple_assignments: bool = True,
807812
) -> None:
808813
# Calculating this would not be needed if TypeMap directly used literal hashes instead of
809814
# expressions, as suggested in the TODO above it's definition
@@ -812,9 +817,10 @@ def update_type_map(
812817
if literal_hash(expr) in already_captured:
813818
if (node := get_var(expr)) is None:
814819
continue
815-
self.msg.fail(
816-
message_registry.MULTIPLE_ASSIGNMENTS_IN_PATTERN.format(node.name), expr
817-
)
820+
if fail_multiple_assignments:
821+
self.msg.fail(
822+
message_registry.MULTIPLE_ASSIGNMENTS_IN_PATTERN.format(node.name), expr
823+
)
818824
else:
819825
original_type_map[expr] = typ
820826

test-data/unit/check-python310.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,10 @@ match (m, n, o):
30743074
case [1, *_, 3, 4, 5] | [2, *_, 3, 4, 5]:
30753075
# No match -> don't crash
30763076
reveal_type(m) # E: Statement is unreachable
3077+
case [m, *_]:
3078+
# This will always match and bind the variables to itself
3079+
# Although it doesn't make much sense, make sure it doesn't raise an error
3080+
reveal_type(m) # N: Revealed type is "builtins.object"
30773081

30783082
match [m, n, o]:
30793083
case [1, 2, 3] | [2, 3, 4]:

0 commit comments

Comments
 (0)