From 6f7a397196fe2a29c7523238df5f7f0d1a745d42 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Fri, 29 May 2015 16:58:58 +0300 Subject: Add a new SuperError exception subclass, SuperArgumentTypeError, which is raised when there's a problem with any argument of the super call (invalid types). --HG-- branch : 1.4.0 --- astroid/exceptions.py | 4 ++++ astroid/objects.py | 15 +++++++++------ astroid/tests/unittest_objects.py | 6 ++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 9e3a279f..47f2fe50 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -46,6 +46,10 @@ class SuperError(ResolveError): """Error raised when there is a problem with a super call.""" +class SuperArgumentTypeError(SuperError): + """Error raised when the super arguments are invalid.""" + + class NotFoundError(ResolveError): """raised when we are unable to resolve a name""" diff --git a/astroid/objects.py b/astroid/objects.py index 42cb00cf..9af7a866 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -34,7 +34,10 @@ from astroid.bases import ( BUILTINS, NodeNG, Instance, _infer_stmts, BoundMethod, UnboundMethod, ) -from astroid.exceptions import SuperError, NotFoundError, MroError +from astroid.exceptions import ( + SuperError, SuperArgumentTypeError, + NotFoundError, MroError +) from astroid.node_classes import const_factory from astroid.scoped_nodes import Class, Function from astroid.mixins import ParentAssignTypeMixin @@ -96,7 +99,7 @@ class Super(NodeNG): def super_mro(self): """Get the MRO which will be used to lookup attributes in this super.""" if not isinstance(self.mro_pointer, Class): - raise SuperError("The first super argument must be type.") + raise SuperArgumentTypeError("The first super argument must be type.") if isinstance(self.type, Class): # `super(type, type)`, most likely in a class method. @@ -105,16 +108,16 @@ class Super(NodeNG): else: mro_type = getattr(self.type, '_proxied', None) if not isinstance(mro_type, (Instance, Class)): - raise SuperError("super(type, obj): obj must be an instance " - "or subtype of type") + raise SuperArgumentTypeError("super(type, obj): obj must be an " + "instance or subtype of type") if not mro_type.newstyle: raise SuperError("Unable to call super on old-style classes.") mro = mro_type.mro() if self.mro_pointer not in mro: - raise SuperError("super(type, obj): obj must be an instance " - "or subtype of type") + raise SuperArgumentTypeError("super(type, obj): obj must be an " + "instance or subtype of type") index = mro.index(self.mro_pointer) return mro[index + 1:] diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index 31f648eb..24844291 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -239,7 +239,7 @@ class SuperTests(unittest.TestCase): for node in ast_nodes[1:]: inferred = next(node.infer()) self.assertIsInstance(inferred, objects.Super, node) - with self.assertRaises(exceptions.SuperError) as cm: + with self.assertRaises(exceptions.SuperArgumentTypeError) as cm: inferred.super_mro() self.assertEqual(str(cm.exception), "super(type, obj): obj must be an instance " @@ -486,7 +486,9 @@ class SuperTests(unittest.TestCase): ''') inferred = next(node.infer()) with self.assertRaises(exceptions.SuperError): - inferred.super_mro() + inferred.super_mro() + with self.assertRaises(exceptions.SuperArgumentTypeError): + inferred.super_mro() if __name__ == '__main__': -- cgit v1.2.1