Skip to content
Closed
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
11 changes: 10 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,16 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
star_expr = None

for t in current_type.items:
match_type, rest_type, captures = self.accept(o, t)
# A diagnostic from one union alternative may not apply to the
# pattern as a whole. For example, in tuple[A, None] | tuple[B, int],
# `case A, None` matches the first alternative; checking the second
# alternative independently would incorrectly report `None` as
# unreachable against `int`.
watcher = self.msg.filter_errors(save_filtered_errors=True)
with watcher:
match_type, rest_type, captures = self.accept(o, t)
if not is_uninhabited(match_type):
self.msg.add_errors(watcher.filtered_errors())
match_types.append(match_type)
rest_types.append(rest_type)
if not is_uninhabited(match_type):
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -3236,6 +3236,32 @@ def f2(m: tuple[int] | tuple[str, str] | tuple[int, Unpack[tuple[str, ...]], int
reveal_type(m) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.str, ...]], builtins.int]"
[builtins fixtures/tuple.pyi]

[case testMatchSequencePatternTupleUnionFalsePositiveUnreachable]
# flags: --strict-equality --warn-unreachable
from typing import Literal

value: tuple[Literal["a"], None] | tuple[Literal["b"], int] = ("a", None)

match value:
case "a", None:
pass
case _:
pass
[builtins fixtures/tuple.pyi]

[case testMatchSequencePatternTupleUnionFalsePositiveUnreachableClassPattern]
# flags: --strict-equality --warn-unreachable
value: tuple[None, int] | tuple[int, None] | tuple[int, int]

match value:
case None, int(y):
pass
case int(x), None:
pass
case int(x), int(y):
pass
[builtins fixtures/tuple.pyi]

[case testMatchSequencePatternTypeVarTupleNotTooShort]
# flags: --strict-equality --warn-unreachable
from typing import Tuple
Expand Down
Loading