@@ -4940,10 +4940,14 @@ def infer_context_dependent(
49404940 self .expr_checker .accept (expr , None , allow_none_return = allow_none_func_call )
49414941 )
49424942
4943- if not msg .has_new_errors () and is_subtype (alt_typ , type_ctx ) and not self .contains_assignment_expr (expr ):
4943+ if (
4944+ not msg .has_new_errors ()
4945+ and is_subtype (alt_typ , type_ctx )
4946+ and not self .contains_assignment_expr (expr )
4947+ ):
49444948 self .store_types (type_map )
49454949 return alt_typ
4946-
4950+
49474951 # If empty fallback didn't work, use results from the original type context.
49484952 self .msg .add_errors (original_messages )
49494953 self .store_types (original_type_map )
@@ -4982,7 +4986,10 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
49824986
49834987 # Return with a value.
49844988 if (
4985- isinstance (s .expr , (CallExpr , ListExpr , TupleExpr , DictExpr , SetExpr , OpExpr , AssignmentExpr ))
4989+ isinstance (
4990+ s .expr ,
4991+ (CallExpr , ListExpr , TupleExpr , DictExpr , SetExpr , OpExpr , AssignmentExpr ),
4992+ )
49864993 or isinstance (s .expr , AwaitExpr )
49874994 and isinstance (s .expr .expr , CallExpr )
49884995 ):
@@ -5059,78 +5066,77 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
50595066
50605067 if self .in_checked_function ():
50615068 self .fail (message_registry .RETURN_VALUE_EXPECTED , s )
5062-
5069+
50635070 def contains_assignment_expr (self , expr : Expression ) -> bool :
50645071 """Check if expression contains any AssignmentExpr (walrus operator)."""
50655072 # Base case: found an assignment expression
50665073 if isinstance (expr , AssignmentExpr ):
50675074 return True
5068-
5075+
50695076 # Recursively check nested expressions in various expression types
5070-
5077+
50715078 # Container expressions
50725079 if isinstance (expr , (TupleExpr , ListExpr , SetExpr )):
50735080 return any (self .contains_assignment_expr (item ) for item in expr .items )
5074-
5081+
50755082 if isinstance (expr , DictExpr ):
50765083 # Check both keys and values
50775084 for k , v in zip (expr .items , expr .values ):
50785085 if self .contains_assignment_expr (k ) or self .contains_assignment_expr (v ):
50795086 return True
50805087 return False
5081-
5088+
50825089 # Binary operations (left and right operands)
50835090 if isinstance (expr , OpExpr ):
5084- return (
5085- self .contains_assignment_expr (expr .left )
5086- or self .contains_assignment_expr (expr .right )
5091+ return self .contains_assignment_expr (expr .left ) or self .contains_assignment_expr (
5092+ expr .right
50875093 )
5088-
5094+
50895095 # Unary operations
50905096 if isinstance (expr , UnaryExpr ):
50915097 return self .contains_assignment_expr (expr .expr )
5092-
5098+
50935099 # Comparison expressions (multiple operands)
50945100 if isinstance (expr , ComparisonExpr ):
50955101 return any (self .contains_assignment_expr (operand ) for operand in expr .operands )
5096-
5102+
50975103 # Function calls (check arguments)
50985104 if isinstance (expr , CallExpr ):
50995105 # Check callee and all arguments
51005106 if self .contains_assignment_expr (expr .callee ):
51015107 return True
51025108 return any (self .contains_assignment_expr (arg ) for arg in expr .args )
5103-
5109+
51045110 # Index expressions (subscripts)
51055111 if isinstance (expr , IndexExpr ):
51065112 if self .contains_assignment_expr (expr .base ):
51075113 return True
51085114 if expr .index is not None :
51095115 return self .contains_assignment_expr (expr .index )
51105116 return False
5111-
5117+
51125118 # Member access
51135119 if isinstance (expr , MemberExpr ):
51145120 return self .contains_assignment_expr (expr .expr )
5115-
5121+
51165122 # Starred expressions (unpacking)
51175123 if isinstance (expr , StarExpr ):
51185124 return self .contains_assignment_expr (expr .expr )
5119-
5125+
51205126 # Await expressions
51215127 if isinstance (expr , AwaitExpr ):
51225128 return self .contains_assignment_expr (expr .expr )
5123-
5129+
51245130 # Yield expressions
51255131 if isinstance (expr , YieldExpr ):
51265132 if expr .expr is not None :
51275133 return self .contains_assignment_expr (expr .expr )
51285134 return False
5129-
5135+
51305136 # Conditional expressions (ternary operator)
51315137 # Note: ConditionalExpr might not be in imports, but if it exists, handle it
51325138 # For now, we'll skip it if it's not imported
5133-
5139+
51345140 # All other expression types (NameExpr, IntExpr, StrExpr, etc.) don't contain nested expressions
51355141 return False
51365142
0 commit comments