Commits

Ronny Pfannschmidt committed 7e43f8e

make finderrors error reporting more flexible

Comments (0)

Files changed (1)

rope/contrib/finderrors.py

         scope = self.scope.get_inner_scope_for_line(node.lineno)
         pyname = scope.lookup(node.id)
         if pyname is None:
-            self._add_error(node, 'Unresolved variable')
+            self._add_error(node, 'Unresolved variable %s(name)s')
         elif self._is_defined_after(scope, pyname, node.lineno):
-            self._add_error(node, 'Defined later')
+            #XXX: bad factoring
+            line = self._is_defined_after(scope, pyname, node.lineno)[1]
+
+            self._add_error(node, '%(name)s is defined later in line %(line)s',
+                            line=line,
+                           )
 
     def _Attribute(self, node):
         if not isinstance(node.ctx, ast.Store):
             if pyname is not None and \
                pyname.get_object() != pyobjects.get_unknown():
                 if node.attr not in pyname.get_object():
-                    self._add_error(node, 'Unresolved attribute')
+                    self._add_error(node, 'Unresolved attribute %(name)s')
         ast.walk(node.value, self)
 
-    def _add_error(self, node, msg):
+    def _add_error(self, node, msg, **kw):
         if isinstance(node, ast.Attribute):
             name = node.attr
         else:
             name = node.id
         if name != 'None':
-            error = Error(node.lineno, msg + ' ' + name)
+            # allows for override
+            kw.setdefault('name', name)
+            error = Error(node.lineno, msg, **kw)
             self.errors.append(error)
 
     def _is_defined_after(self, scope, pyname, lineno):
         if location is not None and location[1] is not None:
             if location[0] == self.pymodule and \
                lineno <= location[1] <= scope.get_end():
-                return True
+                return location
 
 
 class Error(object):
 
-    def __init__(self, lineno, error):
+    def __init__(self, lineno, msg, **kw):
         self.lineno = lineno
-        self.error = error
+        self.msg = msg
+        self.args = kw
+
+    @property
+    def error(self):
+        return self.msg % self.args
 
     def __str__(self):
         return '%s: %s' % (self.lineno, self.error)
+
+    def __repr__(self):
+        return '<Error %s %r>' % (self.lineno, self.error)
+