summaryrefslogtreecommitdiff
path: root/astroid/util.py
diff options
context:
space:
mode:
authorCeridwen <ceridwenv@gmail.com>2015-11-02 00:10:54 -0500
committerCeridwen <ceridwenv@gmail.com>2015-11-02 00:10:54 -0500
commit1b3a23614a3648978e8ed6258280f661caf73690 (patch)
treefe3b110b67e85dd1e44e7903fc62f77fbf090fa7 /astroid/util.py
parenta6baa5f092e933bc5b84849264df988069cee7d9 (diff)
downloadastroid-git-1b3a23614a3648978e8ed6258280f661caf73690.tar.gz
This bookmark adds structured exceptions to astroid.
Major changes: * AstroidError has an __init__ that accepts arbitrary keyword-only arguments for adding information to exceptions, and a __str__ that lazily uses exception attributes to generate a message. The first positional argument to an exception is assigned to .message. The new API should be fully backwards compatible in general. * Some exceptions are combined or renamed; the old names are still available. * The OperationErrors used by pylint are now BadOperationMessages and located in util.py. * The AstroidBuildingException in _data_build stores the SyntaxError in its .error attribute rather than args[0]. * Many places where exceptions are raised have new, hopefully more useful error messages. The only major issue remaining is how to propagate information into decorators.
Diffstat (limited to 'astroid/util.py')
-rw-r--r--astroid/util.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/astroid/util.py b/astroid/util.py
index 20c44d72..90ea7d6d 100644
--- a/astroid/util.py
+++ b/astroid/util.py
@@ -49,6 +49,41 @@ class YES(object):
return self
+class BadOperationMessage(object):
+ """Object which describes a TypeError occurred somewhere in the inference chain
+
+ This is not an exception, but a container object which holds the types and
+ the error which occurred.
+ """
+
+
+class BadUnaryOperationMessage(BadOperationMessage):
+ """Object which describes operational failures on UnaryOps."""
+
+ def __init__(self, operand, op, error):
+ self.operand = operand
+ self.op = op
+ self.error = error
+
+ def __str__(self):
+ operand_type = self.operand.name
+ msg = "bad operand type for unary {}: {}"
+ return msg.format(self.op, operand_type)
+
+
+class BadBinaryOperationMessage(BadOperationMessage):
+ """Object which describes type errors for BinOps."""
+
+ def __init__(self, left_type, op, right_type):
+ self.left_type = left_type
+ self.right_type = right_type
+ self.op = op
+
+ def __str__(self):
+ msg = "unsupported operand type(s) for {}: {!r} and {!r}"
+ return msg.format(self.op, self.left_type.name, self.right_type.name)
+
+
def _instancecheck(cls, other):
wrapped = cls.__wrapped__
other_cls = other.__class__