summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Lorenz <philip@bithub.de>2015-05-04 22:42:57 +0200
committerPhilip Lorenz <philip@bithub.de>2015-05-04 22:42:57 +0200
commit08287a5b40c7a08ea51a1f39bf08dcc64dca28f4 (patch)
treedfd10686427c941466bcd749bb974f59727ca612
parent1d23387b738d3e8b418178f50629317373bc18a7 (diff)
downloadastroid-fix-recursive-attr-lookups.tar.gz
Fix recursive class attribute lookupsfix-recursive-attr-lookups
Commit 83053ac added MRO lookup support for new-style only class hierarchies. Due to a typo old-style / mixed-style class hierarchies were either not traversed recursively or traversed with the wrong context.
-rw-r--r--astroid/scoped_nodes.py2
-rw-r--r--astroid/tests/unittest_scoped_nodes.py22
2 files changed, 21 insertions, 3 deletions
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index 080f0e5..bcd5ba5 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -1156,7 +1156,7 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin):
# for MroError to leak out.
six.raise_from(NotFoundError, MroError)
else:
- ancestors = self.ancestors(context)
+ ancestors = self.ancestors(context=context)
for astroid in ancestors:
if name in astroid:
yield astroid
diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py
index 01f2f88..916b68b 100644
--- a/astroid/tests/unittest_scoped_nodes.py
+++ b/astroid/tests/unittest_scoped_nodes.py
@@ -596,11 +596,21 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase):
self.assertEqual(r_sibling.name, 'YOUPI')
def test_local_attr_ancestors(self):
- klass2 = self.module['YOUPI']
+ module = test_utils.build_module('''
+ class A():
+ def __init__(self): pass
+ class B(A): pass
+ class C(B): pass
+ class D(object): pass
+ class F(): pass
+ class E(F, D): pass
+ ''')
+ # Test old-style (Python 2) / new-style (Python 3+) ancestors lookups
+ klass2 = module['C']
it = klass2.local_attr_ancestors('__init__')
anc_klass = next(it)
self.assertIsInstance(anc_klass, nodes.Class)
- self.assertEqual(anc_klass.name, 'YO')
+ self.assertEqual(anc_klass.name, 'A')
if sys.version_info[0] == 2:
self.assertRaises(StopIteration, partial(next, it))
else:
@@ -612,6 +622,14 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase):
it = klass2.local_attr_ancestors('method')
self.assertRaises(StopIteration, partial(next, it))
+ # Test mixed-style ancestor lookups
+ klass2 = module['E']
+ it = klass2.local_attr_ancestors('__init__')
+ anc_klass = next(it)
+ self.assertIsInstance(anc_klass, nodes.Class)
+ self.assertEqual(anc_klass.name, 'object')
+ self.assertRaises(StopIteration, partial(next, it))
+
def test_local_attr_mro(self):
module = test_utils.build_module('''
class A(object):