@@ -5478,6 +5478,9 @@ def visit_continue_stmt(self, s: ContinueStmt) -> None:
54785478
54795479 def visit_match_stmt (self , s : MatchStmt ) -> None :
54805480 named_subject = self ._make_named_statement_for_match (s )
5481+ # In sync with similar actions elsewhere, narrow the target if
5482+ # we are matching an AssignmentExpr
5483+ unwrapped_subject = collapse_walrus (s .subject )
54815484 with self .binder .frame_context (can_skip = False , fall_through = 0 ):
54825485 subject_type = get_proper_type (self .expr_checker .accept (s .subject ))
54835486
@@ -5513,9 +5516,9 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
55135516 # Maybe the subject type can be inferred from constraints on
55145517 # its attribute/item?
55155518 if pattern_map and named_subject in pattern_map :
5516- pattern_map [s . subject ] = pattern_map [named_subject ]
5519+ pattern_map [unwrapped_subject ] = pattern_map [named_subject ]
55175520 if else_map and named_subject in else_map :
5518- else_map [s . subject ] = else_map [named_subject ]
5521+ else_map [unwrapped_subject ] = else_map [named_subject ]
55195522 pattern_map = self .propagate_up_typemap_info (pattern_map )
55205523 else_map = self .propagate_up_typemap_info (else_map )
55215524 self .remove_capture_conflicts (pattern_type .captures , inferred_types )
@@ -5571,7 +5574,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
55715574 def _make_named_statement_for_match (self , s : MatchStmt ) -> Expression :
55725575 """Construct a fake NameExpr for inference if a match clause is complex."""
55735576 subject = s .subject
5574- if isinstance (subject , ( NameExpr , AssignmentExpr ) ):
5577+ if self . binder . can_put_directly (subject ):
55755578 # Already named - we should infer type of it as given
55765579 return subject
55775580 elif s .subject_dummy is not None :
0 commit comments