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
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6168,7 +6168,8 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
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:
s.is_exhaustive = is_unreachable_map(unmatched_types)
if not s.is_exhaustive and not self.current_node_deferred:
for typ in unmatched_types.values():
self.msg.match_statement_inexhaustive_match(typ, s)

Expand Down
7 changes: 6 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ def accept(self, visitor: StatementVisitor[T]) -> T:


class MatchStmt(Statement):
__slots__ = ("subject", "subject_dummy", "patterns", "guards", "bodies")
__slots__ = ("subject", "subject_dummy", "patterns", "guards", "bodies", "is_exhaustive")

__match_args__ = ("subject", "patterns", "guards", "bodies")

Expand All @@ -2207,6 +2207,10 @@ class MatchStmt(Statement):
patterns: list[ConcretePattern]
guards: list[Expression | None]
bodies: list[Block]
# (Type checking may modify this flag repeatedly to indicate whether the match is
# exhaustive, i.e. whether the implicit fall-through after the last case is unreachable,
# considering the current type information.)
is_exhaustive: bool

def __init__(
self,
Expand All @@ -2222,6 +2226,7 @@ def __init__(
self.patterns = patterns
self.guards = guards
self.bodies = bodies
self.is_exhaustive = False

def accept(self, visitor: StatementVisitor[T]) -> T:
return visitor.visit_match_stmt(self)
Expand Down
3 changes: 3 additions & 0 deletions mypy/partially_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ def visit_match_stmt(self, o: MatchStmt) -> None:
is_catchall = infer_pattern_value(pattern) == ALWAYS_TRUE
if not is_catchall:
self.tracker.next_branch()
if o.is_exhaustive and i == len(o.patterns) - 1:
# The match is exhaustive, so the implicit fall-through is unreachable.
self.tracker.skip_branch()
self.tracker.end_branch_statement()

def visit_func_def(self, o: FuncDef) -> None:
Expand Down
58 changes: 58 additions & 0 deletions test-data/unit/check-possibly-undefined.test
Original file line number Diff line number Diff line change
Expand Up @@ -1230,3 +1230,61 @@ elif not b:
z = 2
y = z
y + 1

[case testExhaustiveMatchNoFallThrough]
# flags: --enable-error-code possibly-undefined
from enum import Enum
from typing import Literal, Union

class Color(Enum):
RED = 1
BLUE = 2

def union_subject(x: Union[int, str]) -> None:
match x:
case int():
y = 1
case str():
y = 2
y # OK

def enum_subject(c: Color) -> None:
match c:
case Color.RED:
y = 1
case Color.BLUE:
y = 2
y # OK

def literal_subject(l: Literal["a", "b"]) -> None:
match l:
case "a":
y = 1
case "b":
y = 2
y # OK

def non_exhaustive(x: Union[int, str, bytes]) -> None:
match x:
case int():
y = 1
case str():
y = 2
y # E: Name "y" may be undefined

def guard_on_last_case(x: Union[int, str], b: bool) -> None:
match x:
case int():
y = 1
case str() if b:
y = 2
y # E: Name "y" may be undefined

def any_subject(x) -> None:
match x:
case int():
y = 1
case str():
y = 2
y # E: Name "y" may be undefined
[builtins fixtures/tuple.pyi]
Loading