Skip to content
This repository
Browse code

Modifying pyflakes 0.4.0 to retain column information in Message obje…

…cts so that pyflakes.vim can continue to use it.
  • Loading branch information...
commit 8f62656c96b7cf66b7ad6cac0d9e3881e55f389f 1 parent 0e136d0
Kevin Watters authored November 17, 2010
51  pyflakes/checker.py
@@ -258,7 +258,7 @@ def check_dead_scopes(self):
258 258
                     for name in undefined:
259 259
                         self.report(
260 260
                             messages.UndefinedExport,
261  
-                            scope['__all__'].source.lineno,
  261
+                            scope['__all__'].source,
262 262
                             name)
263 263
             else:
264 264
                 all = []
@@ -269,7 +269,7 @@ def check_dead_scopes(self):
269 269
                     if not importation.used and importation.name not in all:
270 270
                         self.report(
271 271
                             messages.UnusedImport,
272  
-                            importation.source.lineno,
  272
+                            importation.source,
273 273
                             importation.name)
274 274
 
275 275
 
@@ -341,10 +341,11 @@ def ignore(self, node):
341 341
     # additional node types
342 342
     COMPREHENSION = EXCEPTHANDLER = KEYWORD = handleChildren
343 343
 
344  
-    def addBinding(self, lineno, value, reportRedef=True):
  344
+    def addBinding(self, loc, value, reportRedef=True):
345 345
         '''Called when a binding is altered.
346 346
 
347  
-        - `lineno` is the line of the statement responsible for the change
  347
+        - `loc` is the location (an object with lineno and optionally
  348
+          col_offset attributes) of the statement responsible for the change
348 349
         - `value` is the optional new value, a Binding instance, associated
349 350
           with the binding; if None, the binding is deleted if it exists.
350 351
         - if `reportRedef` is True (default), rebinding while unused will be
@@ -353,7 +354,7 @@ def addBinding(self, lineno, value, reportRedef=True):
353 354
         if (isinstance(self.scope.get(value.name), FunctionDefinition)
354 355
                     and isinstance(value, FunctionDefinition)):
355 356
             self.report(messages.RedefinedFunction,
356  
-                        lineno, value.name, self.scope[value.name].source.lineno)
  357
+                        loc, value.name, self.scope[value.name].source)
357 358
 
358 359
         if not isinstance(self.scope, ClassScope):
359 360
             for scope in self.scopeStack[::-1]:
@@ -364,13 +365,13 @@ def addBinding(self, lineno, value, reportRedef=True):
364 365
                         and reportRedef):
365 366
 
366 367
                     self.report(messages.RedefinedWhileUnused,
367  
-                                lineno, value.name, scope[value.name].source.lineno)
  368
+                                loc, value.name, scope[value.name].source)
368 369
 
369 370
         if isinstance(value, UnBinding):
370 371
             try:
371 372
                 del self.scope[value.name]
372 373
             except KeyError:
373  
-                self.report(messages.UndefinedName, lineno, value.name)
  374
+                self.report(messages.UndefinedName, loc, value.name)
374 375
         else:
375 376
             self.scope[value.name] = value
376 377
 
@@ -416,7 +417,7 @@ def collectLoopVars(n):
416 417
                     # unused ones will get an unused import warning
417 418
                     and self.scope[varn].used):
418 419
                 self.report(messages.ImportShadowedByLoopVar,
419  
-                            node.lineno, varn, self.scope[varn].source.lineno)
  420
+                            node, varn, self.scope[varn].source)
420 421
 
421 422
         self.handleChildren(node)
422 423
 
@@ -429,7 +430,7 @@ def NAME(self, node):
429 430
             # try local scope
430 431
             importStarred = self.scope.importStarred
431 432
             try:
432  
-                self.scope[node.id].used = (self.scope, node.lineno)
  433
+                self.scope[node.id].used = (self.scope, node)
433 434
             except KeyError:
434 435
                 pass
435 436
             else:
@@ -442,7 +443,7 @@ def NAME(self, node):
442 443
                 if not isinstance(scope, FunctionScope):
443 444
                     continue
444 445
                 try:
445  
-                    scope[node.id].used = (self.scope, node.lineno)
  446
+                    scope[node.id].used = (self.scope, node)
446 447
                 except KeyError:
447 448
                     pass
448 449
                 else:
@@ -452,7 +453,7 @@ def NAME(self, node):
452 453
 
453 454
             importStarred = importStarred or self.scopeStack[0].importStarred
454 455
             try:
455  
-                self.scopeStack[0][node.id].used = (self.scope, node.lineno)
  456
+                self.scopeStack[0][node.id].used = (self.scope, node)
456 457
             except KeyError:
457 458
                 if ((not hasattr(__builtin__, node.id))
458 459
                         and node.id not in _MAGIC_GLOBALS
@@ -462,7 +463,7 @@ def NAME(self, node):
462 463
                         # the special name __path__ is valid only in packages
463 464
                         pass
464 465
                     else:
465  
-                        self.report(messages.UndefinedName, node.lineno, node.id)
  466
+                        self.report(messages.UndefinedName, node, node.id)
466 467
         elif isinstance(node.ctx, (_ast.Store, _ast.AugStore)):
467 468
             # if the name hasn't already been defined in the current scope
468 469
             if isinstance(self.scope, FunctionScope) and node.id not in self.scope:
@@ -481,7 +482,7 @@ def NAME(self, node):
481 482
                         self.report(messages.UndefinedLocal,
482 483
                                     scope[node.id].used[1],
483 484
                                     node.id,
484  
-                                    scope[node.id].source.lineno)
  485
+                                    scope[node.id].source)
485 486
                         break
486 487
 
487 488
             if isinstance(node.parent,
@@ -494,13 +495,13 @@ def NAME(self, node):
494 495
                 binding = Assignment(node.id, node)
495 496
             if node.id in self.scope:
496 497
                 binding.used = self.scope[node.id].used
497  
-            self.addBinding(node.lineno, binding)
  498
+            self.addBinding(node, binding)
498 499
         elif isinstance(node.ctx, _ast.Del):
499 500
             if isinstance(self.scope, FunctionScope) and \
500 501
                    node.id in self.scope.globals:
501 502
                 del self.scope.globals[node.id]
502 503
             else:
503  
-                self.addBinding(node.lineno, UnBinding(node.id, node))
  504
+                self.addBinding(node, UnBinding(node.id, node))
504 505
         else:
505 506
             # must be a Param context -- this only happens for names in function
506 507
             # arguments, but these aren't dispatched through here
@@ -516,7 +517,7 @@ def FUNCTIONDEF(self, node):
516 517
         else:
517 518
             for deco in node.decorator_list:
518 519
                 self.handleNode(deco, node)
519  
-        self.addBinding(node.lineno, FunctionDefinition(node.name, node))
  520
+        self.addBinding(node, FunctionDefinition(node.name, node))
520 521
         self.LAMBDA(node)
521 522
 
522 523
     def LAMBDA(self, node):
@@ -533,7 +534,7 @@ def addArgs(arglist):
533 534
                     else:
534 535
                         if arg.id in args:
535 536
                             self.report(messages.DuplicateArgument,
536  
-                                        node.lineno, arg.id)
  537
+                                        node, arg.id)
537 538
                         args.append(arg.id)
538 539
 
539 540
             self.pushFunctionScope()
@@ -544,7 +545,7 @@ def addArgs(arglist):
544 545
             if node.args.kwarg:
545 546
                 args.append(node.args.kwarg)
546 547
             for name in args:
547  
-                self.addBinding(node.lineno, Argument(name, node), reportRedef=False)
  548
+                self.addBinding(node, Argument(name, node), reportRedef=False)
548 549
             if isinstance(node.body, list):
549 550
                 # case for FunctionDefs
550 551
                 for stmt in node.body:
@@ -560,7 +561,7 @@ def checkUnusedAssignments():
560 561
                     if (not binding.used and not name in self.scope.globals
561 562
                         and isinstance(binding, Assignment)):
562 563
                         self.report(messages.UnusedVariable,
563  
-                                    binding.source.lineno, name)
  564
+                                    binding.source, name)
564 565
             self.deferAssignment(checkUnusedAssignments)
565 566
             self.popScope()
566 567
 
@@ -582,7 +583,7 @@ def CLASSDEF(self, node):
582 583
         for stmt in node.body:
583 584
             self.handleNode(stmt, node)
584 585
         self.popScope()
585  
-        self.addBinding(node.lineno, Binding(node.name, node))
  586
+        self.addBinding(node, Binding(node.name, node))
586 587
 
587 588
     def ASSIGN(self, node):
588 589
         self.handleNode(node.value, node)
@@ -602,12 +603,12 @@ def IMPORT(self, node):
602 603
         for alias in node.names:
603 604
             name = alias.asname or alias.name
604 605
             importation = Importation(name, node)
605  
-            self.addBinding(node.lineno, importation)
  606
+            self.addBinding(node, importation)
606 607
 
607 608
     def IMPORTFROM(self, node):
608 609
         if node.module == '__future__':
609 610
             if not self.futuresAllowed:
610  
-                self.report(messages.LateFutureImport, node.lineno,
  611
+                self.report(messages.LateFutureImport, node,
611 612
                             [n.name for n in node.names])
612 613
         else:
613 614
             self.futuresAllowed = False
@@ -615,10 +616,10 @@ def IMPORTFROM(self, node):
615 616
         for alias in node.names:
616 617
             if alias.name == '*':
617 618
                 self.scope.importStarred = True
618  
-                self.report(messages.ImportStarUsed, node.lineno, node.module)
  619
+                self.report(messages.ImportStarUsed, node, node.module)
619 620
                 continue
620 621
             name = alias.asname or alias.name
621 622
             importation = Importation(name, node)
622 623
             if node.module == '__future__':
623  
-                importation.used = (self.scope, node.lineno)
624  
-            self.addBinding(node.lineno, importation)
  624
+                importation.used = (self.scope, node)
  625
+            self.addBinding(node, importation)
58  pyflakes/messages.py
@@ -3,82 +3,84 @@
3 3
 class Message(object):
4 4
     message = ''
5 5
     message_args = ()
6  
-    def __init__(self, filename, lineno):
  6
+    def __init__(self, filename, loc, use_column=True):
7 7
         self.filename = filename
8  
-        self.lineno = lineno
  8
+        self.lineno = loc.lineno
  9
+        self.col = getattr(loc, 'col_offset', None) if use_column else None
  10
+
9 11
     def __str__(self):
10 12
         return '%s:%s: %s' % (self.filename, self.lineno, self.message % self.message_args)
11 13
 
12 14
 
13 15
 class UnusedImport(Message):
14 16
     message = '%r imported but unused'
15  
-    def __init__(self, filename, lineno, name):
16  
-        Message.__init__(self, filename, lineno)
  17
+    def __init__(self, filename, loc, name):
  18
+        Message.__init__(self, filename, loc, use_column=False)
17 19
         self.message_args = (name,)
18 20
 
19 21
 
20 22
 class RedefinedWhileUnused(Message):
21 23
     message = 'redefinition of unused %r from line %r'
22  
-    def __init__(self, filename, lineno, name, orig_lineno):
23  
-        Message.__init__(self, filename, lineno)
24  
-        self.message_args = (name, orig_lineno)
  24
+    def __init__(self, filename, loc, name, orig_loc):
  25
+        Message.__init__(self, filename, loc)
  26
+        self.message_args = (name, orig_loc.lineno)
25 27
 
26 28
 
27 29
 class ImportShadowedByLoopVar(Message):
28 30
     message = 'import %r from line %r shadowed by loop variable'
29  
-    def __init__(self, filename, lineno, name, orig_lineno):
30  
-        Message.__init__(self, filename, lineno)
31  
-        self.message_args = (name, orig_lineno)
  31
+    def __init__(self, filename, loc, name, orig_loc):
  32
+        Message.__init__(self, filename, loc)
  33
+        self.message_args = (name, orig_loc.lineno)
32 34
 
33 35
 
34 36
 class ImportStarUsed(Message):
35 37
     message = "'from %s import *' used; unable to detect undefined names"
36  
-    def __init__(self, filename, lineno, modname):
37  
-        Message.__init__(self, filename, lineno)
  38
+    def __init__(self, filename, loc, modname):
  39
+        Message.__init__(self, filename, loc)
38 40
         self.message_args = (modname,)
39 41
 
40 42
 
41 43
 class UndefinedName(Message):
42 44
     message = 'undefined name %r'
43  
-    def __init__(self, filename, lineno, name):
44  
-        Message.__init__(self, filename, lineno)
  45
+    def __init__(self, filename, loc, name):
  46
+        Message.__init__(self, filename, loc)
45 47
         self.message_args = (name,)
46 48
 
47 49
 
48 50
 
49 51
 class UndefinedExport(Message):
50 52
     message = 'undefined name %r in __all__'
51  
-    def __init__(self, filename, lineno, name):
52  
-        Message.__init__(self, filename, lineno)
  53
+    def __init__(self, filename, loc, name):
  54
+        Message.__init__(self, filename, loc)
53 55
         self.message_args = (name,)
54 56
 
55 57
 
56 58
 
57 59
 class UndefinedLocal(Message):
58 60
     message = "local variable %r (defined in enclosing scope on line %r) referenced before assignment"
59  
-    def __init__(self, filename, lineno, name, orig_lineno):
60  
-        Message.__init__(self, filename, lineno)
61  
-        self.message_args = (name, orig_lineno)
  61
+    def __init__(self, filename, loc, name, orig_loc):
  62
+        Message.__init__(self, filename, loc)
  63
+        self.message_args = (name, orig_loc.lineno)
62 64
 
63 65
 
64 66
 class DuplicateArgument(Message):
65 67
     message = 'duplicate argument %r in function definition'
66  
-    def __init__(self, filename, lineno, name):
67  
-        Message.__init__(self, filename, lineno)
  68
+    def __init__(self, filename, loc, name):
  69
+        Message.__init__(self, filename, loc)
68 70
         self.message_args = (name,)
69 71
 
70 72
 
71 73
 class RedefinedFunction(Message):
72 74
     message = 'redefinition of function %r from line %r'
73  
-    def __init__(self, filename, lineno, name, orig_lineno):
74  
-        Message.__init__(self, filename, lineno)
75  
-        self.message_args = (name, orig_lineno)
  75
+    def __init__(self, filename, loc, name, orig_loc):
  76
+        Message.__init__(self, filename, loc)
  77
+        self.message_args = (name, orig_loc.lineno)
76 78
 
77 79
 
78 80
 class LateFutureImport(Message):
79 81
     message = 'future import(s) %r after other statements'
80  
-    def __init__(self, filename, lineno, names):
81  
-        Message.__init__(self, filename, lineno)
  82
+    def __init__(self, filename, loc, names):
  83
+        Message.__init__(self, filename, loc)
82 84
         self.message_args = (names,)
83 85
 
84 86
 
@@ -89,6 +91,6 @@ class UnusedVariable(Message):
89 91
     """
90 92
 
91 93
     message = 'local variable %r is assigned to but never used'
92  
-    def __init__(self, filename, lineno, names):
93  
-        Message.__init__(self, filename, lineno)
  94
+    def __init__(self, filename, loc, names):
  95
+        Message.__init__(self, filename, loc)
94 96
         self.message_args = (names,)

0 notes on commit 8f62656

Please sign in to comment.
Something went wrong with that request. Please try again.