Skip to content
Open
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
8 changes: 6 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5944,6 +5944,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
# The second pass narrows down the types and type checks bodies.
unmatched_types: TypeMap = {s.subject: UninhabitedType()}
for p, g, b in zip(s.patterns, s.guards, s.bodies):
case_is_unreachable = self.binder.is_unreachable()
current_subject_type = self.expr_checker.narrow_type_from_binder(
named_subject, subject_type
)
Expand Down Expand Up @@ -5996,8 +5997,11 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
self.accept(b)
else:
self.accept(b)
self.push_type_map(else_map, from_assignment=False)
unmatched_types = else_map
if not case_is_unreachable:
# Unreachable cases are still checked above, but they shouldn't revive
# subject types that were already handled by earlier cases.
self.push_type_map(else_map, from_assignment=False)
unmatched_types = else_map

if not is_unreachable_map(unmatched_types) and not self.current_node_deferred:
for typ in unmatched_types.values():
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 @@ -3625,6 +3625,21 @@ match b:
case _:
pass

[case testExhaustiveMatchIgnoresUnreachableClassPattern]
# flags: --enable-error-code exhaustive-match
class A: pass
class B: pass

x: A | None

match x:
case None:
pass
case A() as a:
pass
case B():
pass

[case testNonExhaustiveMatchWithFlag]
# flags: --enable-error-code exhaustive-match --strict-equality --warn-unreachable

Expand Down
Loading