From 37f203ed6ec9c54ea4ded0b8480846ac750a1747 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 27 Nov 2022 12:36:00 -0800 Subject: fold unused checks into final scope checking (#750) --- pyflakes/checker.py | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 15b4c2b..a79de56 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -769,7 +769,6 @@ class Checker: withDoctest='PYFLAKES_DOCTEST' in os.environ, file_tokens=()): self._nodeHandlers = {} self._deferredFunctions = [] - self._deferredAssignments = [] self.deadScopes = [] self.messages = [] self.filename = filename @@ -790,11 +789,8 @@ class Checker: # Set _deferredFunctions to None so that deferFunction will fail # noisily if called after we've run through the deferred functions. self._deferredFunctions = None - self.runDeferred(self._deferredAssignments) - # Set _deferredAssignments to None so that deferAssignment will fail - # noisily if called after we've run through the deferred assignments. - self._deferredAssignments = None del self.scopeStack[1:] + self.popScope() self.checkDeadScopes() @@ -815,13 +811,6 @@ class Checker: """ self._deferredFunctions.append((callable, self.scopeStack[:], self.offset)) - def deferAssignment(self, callable): - """ - Schedule an assignment handler to be called just after deferred - function handlers. - """ - self._deferredAssignments.append((callable, self.scopeStack[:], self.offset)) - def runDeferred(self, deferred): """ Run the callables in C{deferred} using their associated scope stack. @@ -879,6 +868,12 @@ class Checker: if isinstance(scope, ClassScope): continue + if isinstance(scope, FunctionScope): + for name, binding in scope.unused_assignments(): + self.report(messages.UnusedVariable, binding.source, name) + for name, binding in scope.unused_annotations(): + self.report(messages.UnusedAnnotation, binding.source, name) + all_binding = scope.get('__all__') if all_binding and not isinstance(all_binding, ExportBinding): all_binding = None @@ -1991,28 +1986,10 @@ class Checker: self.handleNode(default, node) def runFunction(): - self.pushScope() self.handleChildren(node, omit=['decorator_list', 'returns']) - def check_unused_assignments(): - """ - Check to see if any assignments have not been used. - """ - for name, binding in self.scope.unused_assignments(): - self.report(messages.UnusedVariable, binding.source, name) - - def check_unused_annotations(): - """ - Check to see if any annotations have not been used. - """ - for name, binding in self.scope.unused_annotations(): - self.report(messages.UnusedAnnotation, binding.source, name) - - self.deferAssignment(check_unused_assignments) - self.deferAssignment(check_unused_annotations) - self.popScope() self.deferFunction(runFunction) -- cgit v1.2.1