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
5 changes: 2 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5552,10 +5552,10 @@ def visit_continue_stmt(self, s: ContinueStmt) -> None:
return

def visit_match_stmt(self, s: MatchStmt) -> None:
named_subject = self._make_named_statement_for_match(s)
# In sync with similar actions elsewhere, narrow the target if
# we are matching an AssignmentExpr
unwrapped_subject = collapse_walrus(s.subject)
named_subject = self._make_named_statement_for_match(s, unwrapped_subject)
with self.binder.frame_context(can_skip=False, fall_through=0):
subject_type = get_proper_type(self.expr_checker.accept(s.subject))

Expand Down Expand Up @@ -5646,9 +5646,8 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
with self.binder.frame_context(can_skip=False, fall_through=2):
pass

def _make_named_statement_for_match(self, s: MatchStmt) -> Expression:
def _make_named_statement_for_match(self, s: MatchStmt, subject: Expression) -> Expression:
"""Construct a fake NameExpr for inference if a match clause is complex."""
subject = s.subject
if self.binder.can_put_directly(subject):
# Already named - we should infer type of it as given
return subject
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,16 @@ match m:
reveal_type(a)
[builtins fixtures/isinstancelist.pyi]

[case testMatchSubjectAssignExprWithGuard]
from typing import Optional
def func() -> Optional[str]: ...

match m := func():
case _ if not m:
reveal_type(m) # N: Revealed type is "Union[Literal[''], None]"
case _:
reveal_type(m) # N: Revealed type is "builtins.str"

-- Exhaustiveness --

[case testMatchUnionNegativeNarrowing]
Expand Down
Loading