Commits

Virgil Dupras committed 3d4165b

Code cleanup.

Comments (0)

Files changed (1)

pyflakes/checker.py

         self.scopeStack = [ModuleScope()]
         self.futuresAllowed = True
         self.handleChildren(tree)
-        self._runDeferred(self._deferredFunctions)
+        self.runDeferred(self._deferredFunctions)
         # 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)
+        self.runDeferred(self._deferredAssignments)
         # Set _deferredAssignments to None so that deferAssignment will fail
         # noisly if called after we've run through the deferred assignments.
         self._deferredAssignments = None
         del self.scopeStack[1:]
         self.popScope()
-        self.check_dead_scopes()
+        self.checkDeadScopes()
 
 
     def deferFunction(self, callable):
         self._deferredAssignments.append((callable, self.scopeStack[:]))
 
 
-    def _runDeferred(self, deferred):
+    def runDeferred(self, deferred):
         """
         Run the callables in C{deferred} using their associated scope stack.
         """
             self.scopeStack = scope
             handler()
 
-
+    @property
     def scope(self):
         return self.scopeStack[-1]
-    scope = property(scope)
 
     def popScope(self):
         self.dead_scopes.append(self.scopeStack.pop())
 
-
-    def check_dead_scopes(self):
+    def checkDeadScopes(self):
         """
         Look at scopes which have been fully examined and report names in them
         which were imported but unused.
     def report(self, messageClass, *args, **kwargs):
         self.messages.append(messageClass(self.filename, *args, **kwargs))
     
+    def addBinding(self, lineno, value, reportRedef=True):
+        '''Called when a binding is altered.
+
+        - `lineno` is the line of the statement responsible for the change
+        - `value` is the optional new value, a Binding instance, associated
+          with the binding; if None, the binding is deleted if it exists.
+        - if `reportRedef` is True (default), rebinding while unused will be
+          reported.
+        '''
+        if (isinstance(self.scope.get(value.name), FunctionDefinition)
+                    and isinstance(value, FunctionDefinition)):
+            self.report(messages.RedefinedFunction,
+                        lineno, value.name, self.scope[value.name].source.lineno)
+
+        if not isinstance(self.scope, ClassScope):
+            for scope in self.scopeStack[::-1]:
+                existing = scope.get(value.name)
+                if (isinstance(existing, Importation)
+                        and not existing.used
+                        and (not isinstance(value, Importation) or value.fullName == existing.fullName)
+                        and reportRedef):
+
+                    self.report(messages.RedefinedWhileUnused,
+                                lineno, value.name, scope[value.name].source.lineno)
+
+        if isinstance(value, UnBinding):
+            try:
+                del self.scope[value.name]
+            except KeyError:
+                self.report(messages.UndefinedName, lineno, value.name)
+        else:
+            self.scope[value.name] = value
+    
     def handleNameLoad(self, name, node):
         # try local scope
         importStarred = self.scope.importStarred
                         scope[name].source.lineno)
                     break
 
-        if isinstance(node.parent, (ast.For, ast.comprehension, ast.Tuple, ast.List)):
+        parent = getattr(node, 'parent', None)
+        if isinstance(parent, (ast.For, ast.comprehension, ast.Tuple, ast.List)):
             binding = Binding(name, node)
-        elif (name == '__all__' and isinstance(self.scope, ModuleScope)):
-            binding = ExportBinding(name, node.parent.value)
+        elif parent is not None and name == '__all__' and isinstance(self.scope, ModuleScope):
+            binding = ExportBinding(name, parent.value)
         else:
             binding = Assignment(name, node)
         if name in self.scope:
     # additional node types
     COMPREHENSION = KEYWORD = handleChildren
 
-    def addBinding(self, lineno, value, reportRedef=True):
-        '''Called when a binding is altered.
-
-        - `lineno` is the line of the statement responsible for the change
-        - `value` is the optional new value, a Binding instance, associated
-          with the binding; if None, the binding is deleted if it exists.
-        - if `reportRedef` is True (default), rebinding while unused will be
-          reported.
-        '''
-        if (isinstance(self.scope.get(value.name), FunctionDefinition)
-                    and isinstance(value, FunctionDefinition)):
-            self.report(messages.RedefinedFunction,
-                        lineno, value.name, self.scope[value.name].source.lineno)
-
-        if not isinstance(self.scope, ClassScope):
-            for scope in self.scopeStack[::-1]:
-                existing = scope.get(value.name)
-                if (isinstance(existing, Importation)
-                        and not existing.used
-                        and (not isinstance(value, Importation) or value.fullName == existing.fullName)
-                        and reportRedef):
-
-                    self.report(messages.RedefinedWhileUnused,
-                                lineno, value.name, scope[value.name].source.lineno)
-
-        if isinstance(value, UnBinding):
-            try:
-                del self.scope[value.name]
-            except KeyError:
-                self.report(messages.UndefinedName, lineno, value.name)
-        else:
-            self.scope[value.name] = value
-
     def GLOBAL(self, node):
         """
         Keep track of globals declarations.
 
     GENERATOREXP = SETCOMP = LISTCOMP
 
-    # dictionary comprehensions; introduced in Python 2.7
     def DICTCOMP(self, node):
         for gen in node.generators:
             self.handleNode(gen, node)
             raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
 
     def FUNCTIONDEF(self, node):
-        # the decorators attribute is called decorator_list as of Python 2.6
-        if hasattr(node, 'decorators'):
-            for deco in node.decorators:
-                self.handleNode(deco, node)
-        else:
-            for deco in node.decorator_list:
-                self.handleNode(deco, node)
+        for deco in node.decorator_list:
+            self.handleNode(deco, node)
         self.addBinding(node.lineno, FunctionDefinition(node.name, node))
         self.LAMBDA(node)
 
         classes, and the body of its definition.  Additionally, add its name to
         the current scope.
         """
-        # decorator_list is present as of Python 2.6
-        for deco in getattr(node, 'decorator_list', []):
+        for deco in node.decorator_list:
             self.handleNode(deco, node)
         for baseNode in node.bases:
             self.handleNode(baseNode, node)
             self.handleNode(target, node)
 
     def AUGASSIGN(self, node):
-        # AugAssign is awkward: must set the context explicitly and visit twice,
-        # once with AugLoad context, once with AugStore context
-        node.target.ctx = ast.AugLoad()
-        self.handleNode(node.target, node)
-        self.handleNode(node.value, node)
-        node.target.ctx = ast.AugStore()
-        self.handleNode(node.target, node)
+        self.handleNameLoad(node.target.id, node.target)
+        self.handleNameLoad(node.value.id, node.target)
+        self.handleNameStore(node.target.id, node.target)
 
     def IMPORT(self, node):
         for alias in node.names: