Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
Showing with 56 additions and 53 deletions.
  1. +26 −25 pyflakes/checker.py
  2. +30 −28 pyflakes/messages.py
51 pyflakes/checker.py
View
@@ -258,7 +258,7 @@ def check_dead_scopes(self):
for name in undefined:
self.report(
messages.UndefinedExport,
- scope['__all__'].source.lineno,
+ scope['__all__'].source,
name)
else:
all = []
@@ -269,7 +269,7 @@ def check_dead_scopes(self):
if not importation.used and importation.name not in all:
self.report(
messages.UnusedImport,
- importation.source.lineno,
+ importation.source,
importation.name)
@@ -341,10 +341,11 @@ def ignore(self, node):
# additional node types
COMPREHENSION = EXCEPTHANDLER = KEYWORD = handleChildren
- def addBinding(self, lineno, value, reportRedef=True):
+ def addBinding(self, loc, value, reportRedef=True):
'''Called when a binding is altered.
- - `lineno` is the line of the statement responsible for the change
+ - `loc` is the location (an object with lineno and optionally
+ col_offset attributes) 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
@@ -353,7 +354,7 @@ def addBinding(self, lineno, value, reportRedef=True):
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)
+ loc, value.name, self.scope[value.name].source)
if not isinstance(self.scope, ClassScope):
for scope in self.scopeStack[::-1]:
@@ -364,13 +365,13 @@ def addBinding(self, lineno, value, reportRedef=True):
and reportRedef):
self.report(messages.RedefinedWhileUnused,
- lineno, value.name, scope[value.name].source.lineno)
+ loc, value.name, scope[value.name].source)
if isinstance(value, UnBinding):
try:
del self.scope[value.name]
except KeyError:
- self.report(messages.UndefinedName, lineno, value.name)
+ self.report(messages.UndefinedName, loc, value.name)
else:
self.scope[value.name] = value
@@ -416,7 +417,7 @@ def collectLoopVars(n):
# unused ones will get an unused import warning
and self.scope[varn].used):
self.report(messages.ImportShadowedByLoopVar,
- node.lineno, varn, self.scope[varn].source.lineno)
+ node, varn, self.scope[varn].source)
self.handleChildren(node)
@@ -429,7 +430,7 @@ def NAME(self, node):
# try local scope
importStarred = self.scope.importStarred
try:
- self.scope[node.id].used = (self.scope, node.lineno)
+ self.scope[node.id].used = (self.scope, node)
except KeyError:
pass
else:
@@ -442,7 +443,7 @@ def NAME(self, node):
if not isinstance(scope, FunctionScope):
continue
try:
- scope[node.id].used = (self.scope, node.lineno)
+ scope[node.id].used = (self.scope, node)
except KeyError:
pass
else:
@@ -452,7 +453,7 @@ def NAME(self, node):
importStarred = importStarred or self.scopeStack[0].importStarred
try:
- self.scopeStack[0][node.id].used = (self.scope, node.lineno)
+ self.scopeStack[0][node.id].used = (self.scope, node)
except KeyError:
if ((not hasattr(__builtin__, node.id))
and node.id not in _MAGIC_GLOBALS
@@ -462,7 +463,7 @@ def NAME(self, node):
# the special name __path__ is valid only in packages
pass
else:
- self.report(messages.UndefinedName, node.lineno, node.id)
+ self.report(messages.UndefinedName, node, node.id)
elif isinstance(node.ctx, (_ast.Store, _ast.AugStore)):
# if the name hasn't already been defined in the current scope
if isinstance(self.scope, FunctionScope) and node.id not in self.scope:
@@ -481,7 +482,7 @@ def NAME(self, node):
self.report(messages.UndefinedLocal,
scope[node.id].used[1],
node.id,
- scope[node.id].source.lineno)
+ scope[node.id].source)
break
if isinstance(node.parent,
@@ -494,13 +495,13 @@ def NAME(self, node):
binding = Assignment(node.id, node)
if node.id in self.scope:
binding.used = self.scope[node.id].used
- self.addBinding(node.lineno, binding)
+ self.addBinding(node, binding)
elif isinstance(node.ctx, _ast.Del):
if isinstance(self.scope, FunctionScope) and \
node.id in self.scope.globals:
del self.scope.globals[node.id]
else:
- self.addBinding(node.lineno, UnBinding(node.id, node))
+ self.addBinding(node, UnBinding(node.id, node))
else:
# must be a Param context -- this only happens for names in function
# arguments, but these aren't dispatched through here
@@ -516,7 +517,7 @@ def FUNCTIONDEF(self, node):
else:
for deco in node.decorator_list:
self.handleNode(deco, node)
- self.addBinding(node.lineno, FunctionDefinition(node.name, node))
+ self.addBinding(node, FunctionDefinition(node.name, node))
self.LAMBDA(node)
def LAMBDA(self, node):
@@ -533,7 +534,7 @@ def addArgs(arglist):
else:
if arg.id in args:
self.report(messages.DuplicateArgument,
- node.lineno, arg.id)
+ node, arg.id)
args.append(arg.id)
self.pushFunctionScope()
@@ -544,7 +545,7 @@ def addArgs(arglist):
if node.args.kwarg:
args.append(node.args.kwarg)
for name in args:
- self.addBinding(node.lineno, Argument(name, node), reportRedef=False)
+ self.addBinding(node, Argument(name, node), reportRedef=False)
if isinstance(node.body, list):
# case for FunctionDefs
for stmt in node.body:
@@ -560,7 +561,7 @@ def checkUnusedAssignments():
if (not binding.used and not name in self.scope.globals
and isinstance(binding, Assignment)):
self.report(messages.UnusedVariable,
- binding.source.lineno, name)
+ binding.source, name)
self.deferAssignment(checkUnusedAssignments)
self.popScope()
@@ -582,7 +583,7 @@ def CLASSDEF(self, node):
for stmt in node.body:
self.handleNode(stmt, node)
self.popScope()
- self.addBinding(node.lineno, Binding(node.name, node))
+ self.addBinding(node, Binding(node.name, node))
def ASSIGN(self, node):
self.handleNode(node.value, node)
@@ -602,12 +603,12 @@ def IMPORT(self, node):
for alias in node.names:
name = alias.asname or alias.name
importation = Importation(name, node)
- self.addBinding(node.lineno, importation)
+ self.addBinding(node, importation)
def IMPORTFROM(self, node):
if node.module == '__future__':
if not self.futuresAllowed:
- self.report(messages.LateFutureImport, node.lineno,
+ self.report(messages.LateFutureImport, node,
[n.name for n in node.names])
else:
self.futuresAllowed = False
@@ -615,10 +616,10 @@ def IMPORTFROM(self, node):
for alias in node.names:
if alias.name == '*':
self.scope.importStarred = True
- self.report(messages.ImportStarUsed, node.lineno, node.module)
+ self.report(messages.ImportStarUsed, node, node.module)
continue
name = alias.asname or alias.name
importation = Importation(name, node)
if node.module == '__future__':
- importation.used = (self.scope, node.lineno)
- self.addBinding(node.lineno, importation)
+ importation.used = (self.scope, node)
+ self.addBinding(node, importation)
58 pyflakes/messages.py
View
@@ -3,82 +3,84 @@
class Message(object):
message = ''
message_args = ()
- def __init__(self, filename, lineno):
+ def __init__(self, filename, loc, use_column=True):
self.filename = filename
- self.lineno = lineno
+ self.lineno = loc.lineno
+ self.col = getattr(loc, 'col_offset', None) if use_column else None
+
def __str__(self):
return '%s:%s: %s' % (self.filename, self.lineno, self.message % self.message_args)
class UnusedImport(Message):
message = '%r imported but unused'
- def __init__(self, filename, lineno, name):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, name):
+ Message.__init__(self, filename, loc, use_column=False)
self.message_args = (name,)
class RedefinedWhileUnused(Message):
message = 'redefinition of unused %r from line %r'
- def __init__(self, filename, lineno, name, orig_lineno):
- Message.__init__(self, filename, lineno)
- self.message_args = (name, orig_lineno)
+ def __init__(self, filename, loc, name, orig_loc):
+ Message.__init__(self, filename, loc)
+ self.message_args = (name, orig_loc.lineno)
class ImportShadowedByLoopVar(Message):
message = 'import %r from line %r shadowed by loop variable'
- def __init__(self, filename, lineno, name, orig_lineno):
- Message.__init__(self, filename, lineno)
- self.message_args = (name, orig_lineno)
+ def __init__(self, filename, loc, name, orig_loc):
+ Message.__init__(self, filename, loc)
+ self.message_args = (name, orig_loc.lineno)
class ImportStarUsed(Message):
message = "'from %s import *' used; unable to detect undefined names"
- def __init__(self, filename, lineno, modname):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, modname):
+ Message.__init__(self, filename, loc)
self.message_args = (modname,)
class UndefinedName(Message):
message = 'undefined name %r'
- def __init__(self, filename, lineno, name):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, name):
+ Message.__init__(self, filename, loc)
self.message_args = (name,)
class UndefinedExport(Message):
message = 'undefined name %r in __all__'
- def __init__(self, filename, lineno, name):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, name):
+ Message.__init__(self, filename, loc)
self.message_args = (name,)
class UndefinedLocal(Message):
message = "local variable %r (defined in enclosing scope on line %r) referenced before assignment"
- def __init__(self, filename, lineno, name, orig_lineno):
- Message.__init__(self, filename, lineno)
- self.message_args = (name, orig_lineno)
+ def __init__(self, filename, loc, name, orig_loc):
+ Message.__init__(self, filename, loc)
+ self.message_args = (name, orig_loc.lineno)
class DuplicateArgument(Message):
message = 'duplicate argument %r in function definition'
- def __init__(self, filename, lineno, name):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, name):
+ Message.__init__(self, filename, loc)
self.message_args = (name,)
class RedefinedFunction(Message):
message = 'redefinition of function %r from line %r'
- def __init__(self, filename, lineno, name, orig_lineno):
- Message.__init__(self, filename, lineno)
- self.message_args = (name, orig_lineno)
+ def __init__(self, filename, loc, name, orig_loc):
+ Message.__init__(self, filename, loc)
+ self.message_args = (name, orig_loc.lineno)
class LateFutureImport(Message):
message = 'future import(s) %r after other statements'
- def __init__(self, filename, lineno, names):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, names):
+ Message.__init__(self, filename, loc)
self.message_args = (names,)
@@ -89,6 +91,6 @@ class UnusedVariable(Message):
"""
message = 'local variable %r is assigned to but never used'
- def __init__(self, filename, lineno, names):
- Message.__init__(self, filename, lineno)
+ def __init__(self, filename, loc, names):
+ Message.__init__(self, filename, loc)
self.message_args = (names,)
Please sign in to comment.
Something went wrong with that request. Please try again.