diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2017-07-08 13:41:24 +0300 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2017-07-08 13:41:24 +0300 |
commit | 573b900773d249fbb2858c1a19db3f1434abbd42 (patch) | |
tree | ef694da8aa2d2abb9dd08723815dc5d1c0af34dd /astroid/util.py | |
parent | 03f3ea786885d794d3cdeaa82df4ce36d60d68e1 (diff) | |
download | astroid-git-573b900773d249fbb2858c1a19db3f1434abbd42.tar.gz |
Don't crash when getting the string representation of BadUnaryOperationMessage
In some cases, when the operand does not have a .name attribute,
getting the string representation of a BadUnaryOperationMessage leads
to a crash.
Close PyCQA/pylint#1563
Diffstat (limited to 'astroid/util.py')
-rw-r--r-- | astroid/util.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/astroid/util.py b/astroid/util.py index 6d2d0177..6fd52d31 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -74,8 +74,29 @@ class BadUnaryOperationMessage(BadOperationMessage): self.op = op self.error = error + @property + def _object_type_helper(self): + helpers = lazy_import('helpers') + return helpers.object_type + + def _object_type(self, obj): + objtype = self._object_type_helper(obj) + if objtype is Uninferable: + return None + + return objtype + def __str__(self): - operand_type = self.operand.name + if hasattr(self.operand, 'name'): + operand_type = self.operand.name + else: + object_type = self._object_type(self.operand) + if hasattr(object_type, 'name'): + operand_type = object_type.name + else: + # Just fallback to as_string + operand_type = object_type.as_string() + msg = "bad operand type for unary {}: {}" return msg.format(self.op, operand_type) |