@@ -409,7 +409,6 @@ class FunctionScope(Scope):
409
409
410
410
@ivar globals: Names declared 'global' in this function.
411
411
"""
412
- usesLocals = False
413
412
alwaysUsed = {'__tracebackhide__' , '__traceback_info__' ,
414
413
'__traceback_supplement__' }
415
414
@@ -426,7 +425,6 @@ def unusedAssignments(self):
426
425
"""
427
426
for name , binding in self .items ():
428
427
if (not binding .used and name not in self .globals
429
- and not self .usesLocals
430
428
and isinstance (binding , Assignment )):
431
429
yield name , binding
432
430
@@ -708,6 +706,15 @@ def handleNodeLoad(self, node):
708
706
in_generators = None
709
707
importStarred = None
710
708
709
+ if node .id == 'locals' and isinstance (node .parent , ast .Call ):
710
+ # we are doing locals() call, which marks names currently
711
+ # in scope as used.
712
+ scope = self .scope
713
+ if isinstance (scope , GeneratorScope ):
714
+ scope = self .scopeStack [- 2 ]
715
+ for binding in scope .values ():
716
+ binding .used = (self .scope , node )
717
+
711
718
# try enclosing function scopes and global scope
712
719
for scope in self .scopeStack [- 1 ::- 1 ]:
713
720
if isinstance (scope , ClassScope ):
@@ -1094,10 +1101,6 @@ def NAME(self, node):
1094
1101
# Locate the name in locals / function / globals scopes.
1095
1102
if isinstance (node .ctx , (ast .Load , ast .AugLoad )):
1096
1103
self .handleNodeLoad (node )
1097
- if (node .id == 'locals' and isinstance (self .scope , FunctionScope )
1098
- and isinstance (node .parent , ast .Call )):
1099
- # we are doing locals() call in current scope
1100
- self .scope .usesLocals = True
1101
1104
elif isinstance (node .ctx , (ast .Store , ast .AugStore )):
1102
1105
self .handleNodeStore (node )
1103
1106
elif isinstance (node .ctx , ast .Del ):
0 commit comments