Ticket #2718: unused-in-function-2718.diff
| File unused-in-function-2718.diff, 4.6 kB (added by jml, 1 year ago) |
|---|
-
pyflakes/checker.py
old new 35 35 name = name.split('.')[0] 36 36 super(Importation, self).__init__(name, source) 37 37 38 39 class Argument(Binding): 40 pass 41 42 38 43 class Assignment(Binding): 39 44 pass 40 45 46 41 47 class FunctionDefinition(Binding): 42 48 pass 43 49 … … 316 322 self.pushFunctionScope() 317 323 addArgs(node.argnames) 318 324 for name in args: 319 self.addBinding(node.lineno, A ssignment(name, node), reportRedef=False)325 self.addBinding(node.lineno, Argument(name, node), reportRedef=False) 320 326 self.handleNode(node.code) 327 for name, binding in self.scope.iteritems(): 328 if (not binding.used and not name in self.scope.globals 329 and isinstance(binding, Assignment)): 330 self.report(messages.UnusedVariable, 331 binding.source.lineno, name) 321 332 self.popScope() 322 333 323 334 self.defer(runFunction) 324 335 325 336 def CLASS(self, node): 326 self.addBinding(node.lineno, Assignment(node.name, node))337 self.addBinding(node.lineno, Binding(node.name, node)) 327 338 for baseNode in node.bases: 328 339 self.handleNode(baseNode) 329 340 self.pushClassScope() -
pyflakes/messages.py
old new 71 71 def __init__(self, filename, lineno, names): 72 72 Message.__init__(self, filename, lineno) 73 73 self.message_args = (names,) 74 75 76 class UnusedVariable(Message): 77 message = 'local variable %r is assigned to but never used' 78 def __init__(self, filename, lineno, names): 79 Message.__init__(self, filename, lineno) 80 self.message_args = (names,) -
pyflakes/test/test_imports.py
old new 401 401 import fu 402 402 def a(): 403 403 fu = 3 404 return fu 404 405 fu 405 406 ''') 406 407 -
pyflakes/test/test_other.py
old new 74 74 self.flakes('+1') 75 75 76 76 77 def test_unusedVariable(self): 78 """ 79 Warn when a variable in a function is assigned a value that's never 80 used. 81 """ 82 self.flakes(''' 83 def a(): 84 b = 1 85 ''', m.UnusedVariable) 86 87 88 def test_assignToGlobal(self): 89 """ 90 Assigning to a global and then not using that global is perfectly 91 acceptable. Do not mistake it for an unused local variable. 92 """ 93 self.flakes(''' 94 b = 0 95 def a(): 96 global b 97 b = 1 98 ''') 99 100 def test_assignToMember(self): 101 """ 102 Assigning to a member of another object and then not using that member 103 variable is perfectly acceptable. Do not mistake it for an unused 104 local variable. 105 """ 106 # XXX: Adding this test didn't generate a failure. Maybe not 107 # necessary? 108 self.flakes(''' 109 class b: 110 pass 111 def a(): 112 b.foo = 1 113 ''') 114 115 77 116 78 117 class Python25Test(harness.Test): 79 118 """ -
pyflakes/test/test_undefined_names.py
old new 91 91 def fun(): 92 92 a 93 93 a = 2 94 return a 94 95 ''', m.UndefinedLocal) 95 96 96 97 def test_laterRedefinedGlobalFromNestedScope2(self): … … 106 107 def fun2(): 107 108 a 108 109 a = 2 110 return a 109 111 ''', m.UndefinedLocal) 110 112 111 113 … … 124 126 def c(): 125 127 x 126 128 x = 3 129 return x 130 return x 131 return x 127 132 ''', m.UndefinedLocal).messages[0] 128 133 self.assertEqual(exc.message_args, ('x', 5)) 129 134 … … 139 144 def fun2(): 140 145 a 141 146 a = 1 147 return a 148 return a 142 149 ''', m.UndefinedLocal) 143 150 144 151 def test_nestedClass(self): … … 161 168 class C: 162 169 bar = foo 163 170 foo = 456 164 171 return foo 165 172 f() 166 173 ''', m.UndefinedName) 167 174
