Skip to content

Commit a4df749

Browse files
Kunal SaliKunal Sali
authored andcommitted
Fix: Handle all expression types in contains_assignment_expr for walrus operator detection
- Add missing imports: ConditionalExpr, GeneratorExpr, ListComprehension, SetComprehension, DictionaryComprehension, SliceExpr - Handle ConditionalExpr (ternary operator) to check cond, if_expr, and else_expr - Handle SliceExpr to check begin_index, end_index, and stride - Handle GeneratorExpr and comprehensions (ListComprehension, SetComprehension, DictionaryComprehension) - Ensures comprehensive detection of assignment expressions (walrus operators) in all nested expression contexts - Fixes CI failures related to incomplete expression type handling
1 parent b2e5ccf commit a4df749

1 file changed

Lines changed: 54 additions & 5 deletions

File tree

mypy/checker.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@
8585
CallExpr,
8686
ClassDef,
8787
ComparisonExpr,
88+
ConditionalExpr,
8889
Context,
8990
ContinueStmt,
9091
Decorator,
9192
DelStmt,
9293
DictExpr,
94+
DictionaryComprehension,
9395
EllipsisExpr,
9496
Expression,
9597
ExpressionStmt,
@@ -98,6 +100,7 @@
98100
FuncBase,
99101
FuncDef,
100102
FuncItem,
103+
GeneratorExpr,
101104
GlobalDecl,
102105
IfStmt,
103106
Import,
@@ -107,6 +110,7 @@
107110
IndexExpr,
108111
IntExpr,
109112
LambdaExpr,
113+
ListComprehension,
110114
ListExpr,
111115
Lvalue,
112116
MatchStmt,
@@ -124,7 +128,9 @@
124128
RaiseStmt,
125129
RefExpr,
126130
ReturnStmt,
131+
SetComprehension,
127132
SetExpr,
133+
SliceExpr,
128134
StarExpr,
129135
Statement,
130136
StrExpr,
@@ -5132,11 +5138,54 @@ def contains_assignment_expr(self, expr: Expression) -> bool:
51325138
if expr.expr is not None:
51335139
return self.contains_assignment_expr(expr.expr)
51345140
return False
5135-
5136-
# Conditional expressions (ternary operator)
5137-
# Note: ConditionalExpr might not be in imports, but if it exists, handle it
5138-
# For now, we'll skip it if it's not imported
5139-
5141+
5142+
# Conditional expressions (ternary operator: x if cond else y)
5143+
if isinstance(expr, ConditionalExpr):
5144+
return (
5145+
self.contains_assignment_expr(expr.cond)
5146+
or self.contains_assignment_expr(expr.if_expr)
5147+
or self.contains_assignment_expr(expr.else_expr)
5148+
)
5149+
5150+
# Slice expressions (x:y:z)
5151+
if isinstance(expr, SliceExpr):
5152+
return (
5153+
(expr.begin_index is not None and self.contains_assignment_expr(expr.begin_index))
5154+
or (expr.end_index is not None and self.contains_assignment_expr(expr.end_index))
5155+
or (expr.stride is not None and self.contains_assignment_expr(expr.stride))
5156+
)
5157+
5158+
# Generator expressions and comprehensions
5159+
if isinstance(expr, GeneratorExpr):
5160+
if self.contains_assignment_expr(expr.left_expr):
5161+
return True
5162+
for seq in expr.sequences:
5163+
if self.contains_assignment_expr(seq):
5164+
return True
5165+
for condlist in expr.condlists:
5166+
for cond in condlist:
5167+
if self.contains_assignment_expr(cond):
5168+
return True
5169+
return False
5170+
5171+
if isinstance(expr, ListComprehension):
5172+
return self.contains_assignment_expr(expr.generator)
5173+
5174+
if isinstance(expr, SetComprehension):
5175+
return self.contains_assignment_expr(expr.generator)
5176+
5177+
if isinstance(expr, DictionaryComprehension):
5178+
if self.contains_assignment_expr(expr.key) or self.contains_assignment_expr(expr.value):
5179+
return True
5180+
for seq in expr.sequences:
5181+
if self.contains_assignment_expr(seq):
5182+
return True
5183+
for condlist in expr.condlists:
5184+
for cond in condlist:
5185+
if self.contains_assignment_expr(cond):
5186+
return True
5187+
return False
5188+
51405189
# All other expression types (NameExpr, IntExpr, StrExpr, etc.) don't contain nested expressions
51415190
return False
51425191

0 commit comments

Comments
 (0)