Ticket #2718: unused-in-function-2718-2-progress.diff
| File unused-in-function-2718-2-progress.diff, 5.4 kB (added by jml, 1 year ago) |
|---|
-
pyflakes/checker.py
old new 8 8 from pyflakes import messages 9 9 10 10 11 11 12 class Binding(object): 12 13 """ 14 Represents the binding of a value to a name. 15 16 The checker uses this to keep track of which names have been bound and 17 which names have not. See L{Assignment} for a special type of binding that 18 is checked with stricter rules. 19 13 20 @ivar used: pair of (L{Scope}, line-number) indicating the scope and 14 21 line number that this binding was last used 15 22 """ 23 16 24 def __init__(self, name, source): 17 25 self.name = name 18 26 self.source = source 19 27 self.used = False 20 28 29 21 30 def __str__(self): 22 31 return self.name 23 32 33 24 34 def __repr__(self): 25 35 return '<%s object %r from line %r at 0x%x>' % (self.__class__.__name__, 26 36 self.name, 27 37 self.source.lineno, 28 38 id(self)) 29 39 40 41 30 42 class UnBinding(Binding): 31 43 '''Created by the 'del' operator.''' 32 44 … … 36 48 super(Importation, self).__init__(name, source) 37 49 38 50 51 39 52 class Argument(Binding): 40 pass 53 """ 54 Represents binding a name as an argument. 55 """ 56 41 57 42 58 43 59 class Assignment(Binding): 44 pass 60 """ 61 Represents binding a name with an explicit assignment. 62 63 The checker will raise warnings for any Assignment that isn't used. Also, 64 the checker does not consider assignments in tuple/list unpacking to be 65 Assignments, rather it treats them as simple Bindings. 66 """ 67 45 68 46 69 47 70 class FunctionDefinition(Binding): 48 71 pass 49 72 50 73 74 51 75 class Scope(dict): 52 76 importStarred = False # set to True when import * is found 53 77 78 54 79 def __repr__(self): 55 80 return '<%s at 0x%x %s>' % (self.__class__.__name__, id(self), dict.__repr__(self)) 56 81 82 57 83 def __init__(self): 58 84 super(Scope, self).__init__() 59 85 86 87 60 88 class ClassScope(Scope): 61 89 pass 62 90 … … 78 106 pass 79 107 80 108 109 81 110 class Checker(object): 82 111 nodeDepth = 0 83 112 traceTree = False 84 113 85 114 def __init__(self, tree, filename='(none)'): 86 self._deferred _functions = []87 self._deferred _assignments = []115 self._deferredFunctions = [] 116 self._deferredAssignments = [] 88 117 self.dead_scopes = [] 89 118 self.messages = [] 90 119 self.filename = filename 91 120 self.scopeStack = [ModuleScope()] 92 121 self.futuresAllowed = True 93 122 self.handleChildren(tree) 94 self._runDeferred(self._deferred _functions)95 self._runDeferred(self._deferred _assignments)123 self._runDeferred(self._deferredFunctions) 124 self._runDeferred(self._deferredAssignments) 96 125 del self.scopeStack[1:] 97 126 self.popScope() 98 127 self.check_dead_scopes() … … 107 136 `callable` is called, the scope at the time this is called will be 108 137 restored, however it will contain any new bindings added to it. 109 138 ''' 110 self._deferred _functions.append((callable, self.scopeStack[:]))139 self._deferredFunctions.append((callable, self.scopeStack[:])) 111 140 112 141 113 142 def deferAssignment(self, callable): … … 115 144 Schedule an assignment handler to be called after just before 116 145 completion. 117 146 """ 118 self._deferred _assignments.append((callable, self.scopeStack[:]))147 self._deferredAssignments.append((callable, self.scopeStack[:])) 119 148 120 149 121 150 def _runDeferred(self, deferred): 151 """ 152 Run the callables in C{deferred} using their associated scope stack. 153 """ 122 154 for handler, scope in deferred: 123 155 self.scopeStack = scope 124 156 handler() … … 342 374 self.addBinding(node.lineno, Argument(name, node), reportRedef=False) 343 375 self.handleNode(node.code, node) 344 376 def checkUnusedAssignments(): 377 """ 378 Check to see if any assignments have not been used. 379 """ 345 380 for name, binding in self.scope.iteritems(): 346 381 if (not binding.used and not name in self.scope.globals 347 382 and isinstance(binding, Assignment)): -
pyflakes/messages.py
old new 74 74 75 75 76 76 class UnusedVariable(Message): 77 """ 78 Indicates that a variable has been explicity assigned to but not actually 79 used. 80 """ 81 77 82 message = 'local variable %r is assigned to but never used' 78 83 def __init__(self, filename, lineno, names): 79 84 Message.__init__(self, filename, lineno) -
pyflakes/test/test_other.py
old new 188 188 189 189 190 190 def test_closedOver(self): 191 """ 192 Don't warn when the assignment is used in an inner function. 193 """ 191 194 self.flakes(''' 192 195 def barMaker(): 193 196 foo = 5 … … 198 201 199 202 200 203 def test_doubleClosedOver(self): 204 """ 205 Don't warn when the assignment is used in an inner function, even if 206 that inner function itself is in an inner function. 207 """ 201 208 self.flakes(''' 202 209 def barMaker(): 203 210 foo = 5
