@@ -409,7 +409,6 @@ class FunctionScope(Scope):
409409
410410 @ivar globals: Names declared 'global' in this function.
411411 """
412- usesLocals = False
413412 alwaysUsed = {'__tracebackhide__' , '__traceback_info__' ,
414413 '__traceback_supplement__' }
415414
@@ -428,7 +427,6 @@ def unusedAssignments(self):
428427 if (not binding .used
429428 and name != '_' # see issue #202
430429 and name not in self .globals
431- and not self .usesLocals
432430 and isinstance (binding , Assignment )):
433431 yield name , binding
434432
@@ -710,6 +708,15 @@ def handleNodeLoad(self, node):
710708 in_generators = None
711709 importStarred = None
712710
711+ if node .id == 'locals' and isinstance (node .parent , ast .Call ):
712+ # we are doing locals() call, which marks names currently
713+ # in scope as used.
714+ scope = self .scope
715+ if isinstance (scope , GeneratorScope ):
716+ scope = self .scopeStack [- 2 ]
717+ for binding in scope .values ():
718+ binding .used = (self .scope , node )
719+
713720 # try enclosing function scopes and global scope
714721 for scope in self .scopeStack [- 1 ::- 1 ]:
715722 if isinstance (scope , ClassScope ):
@@ -1096,10 +1103,6 @@ def NAME(self, node):
10961103 # Locate the name in locals / function / globals scopes.
10971104 if isinstance (node .ctx , (ast .Load , ast .AugLoad )):
10981105 self .handleNodeLoad (node )
1099- if (node .id == 'locals' and isinstance (self .scope , FunctionScope )
1100- and isinstance (node .parent , ast .Call )):
1101- # we are doing locals() call in current scope
1102- self .scope .usesLocals = True
11031106 elif isinstance (node .ctx , (ast .Store , ast .AugStore )):
11041107 self .handleNodeStore (node )
11051108 elif isinstance (node .ctx , ast .Del ):
0 commit comments