@@ -540,6 +540,12 @@ class Assignment(Binding):
540
540
"""
541
541
542
542
543
+ class NamedExprAssignment (Assignment ):
544
+ """
545
+ Represents binding a name with an assignment expression.
546
+ """
547
+
548
+
543
549
class Annotation (Binding ):
544
550
"""
545
551
Represents binding a name to a type without an associated value.
@@ -1159,7 +1165,14 @@ def addBinding(self, node, value):
1159
1165
# don't treat annotations as assignments if there is an existing value
1160
1166
# in scope
1161
1167
if value .name not in self .scope or not isinstance (value , Annotation ):
1162
- self .scope [value .name ] = value
1168
+ cur_scope_pos = - 1
1169
+ # As per PEP 572, use scope in which outermost generator is defined
1170
+ while (
1171
+ isinstance (value , NamedExprAssignment ) and
1172
+ isinstance (self .scopeStack [cur_scope_pos ], GeneratorScope )
1173
+ ):
1174
+ cur_scope_pos -= 1
1175
+ self .scopeStack [cur_scope_pos ][value .name ] = value
1163
1176
1164
1177
def _unknown_handler (self , node ):
1165
1178
# this environment variable configures whether to error on unknown
@@ -1302,6 +1315,8 @@ def handleNodeStore(self, node):
1302
1315
binding = ExportBinding (name , node ._pyflakes_parent , self .scope )
1303
1316
elif PY2 and isinstance (getattr (node , 'ctx' , None ), ast .Param ):
1304
1317
binding = Argument (name , self .getScopeNode (node ))
1318
+ elif PY38_PLUS and isinstance (parent_stmt , ast .NamedExpr ):
1319
+ binding = NamedExprAssignment (name , node )
1305
1320
else :
1306
1321
binding = Assignment (name , node )
1307
1322
self .addBinding (node , binding )
0 commit comments