summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Haigh <hello@nelf.in>2021-05-24 19:21:30 +1000
committerGitHub <noreply@github.com>2021-05-24 11:21:30 +0200
commit53a20335357bbd734d74c3bfe22e3518f74f4d11 (patch)
treedc601ff8ad0652fb6a645e54cbcdeced466459b2
parent2e8417ffc2285e798ccdef86f743abb75958e2c6 (diff)
downloadastroid-git-53a20335357bbd734d74c3bfe22e3518f74f4d11.tar.gz
Fix inference cycle in getattr when a base is an astroid.Attribute (#934)
* Fix inference cycle in getattr when a base is an astroid.Attribute Ref #904. `getattr` attempts to find values on `self.ancestors`, which infers each node listed in `self.bases`. Removing or resetting the context passed to ancestors allows it to infer the nodes, but opens up the possibility of cycles through recursive definitions. We should be able to infer the value of the base node correctly as a `ClassDef`. The root of this issue stems from the fact that `infer_attribute` attempts to temporarily set `context.boundnode`, but when unwrapping its changes it sets the `boundnode` to `None` instead of its previous value, which loses state and causes the inference of the `Attribute` node to have a duplicate key: once when looking up the class definition (which should have `boundnode = None`), and once when inferring the (other) attribute value (which should not have `boundnode = None`) * Update changelog
-rw-r--r--ChangeLog4
-rw-r--r--astroid/inference.py3
-rw-r--r--tests/unittest_inference.py59
3 files changed, 65 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 6097526b..c606b761 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -41,6 +41,10 @@ Release Date: TBA
Closes #922
+* Fix inference of attributes defined in a base class that is an inner class
+
+ Closes #904
+
What's New in astroid 2.5.6?
============================
diff --git a/astroid/inference.py b/astroid/inference.py
index 20c98647..dd9a565a 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -309,6 +309,7 @@ def infer_attribute(self, context=None):
elif not context:
context = contextmod.InferenceContext()
+ old_boundnode = context.boundnode
try:
context.boundnode = owner
yield from owner.igetattr(self.attrname, context)
@@ -319,7 +320,7 @@ def infer_attribute(self, context=None):
):
pass
finally:
- context.boundnode = None
+ context.boundnode = old_boundnode
return dict(node=self, context=context)
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index 7fb1ed55..6b9f4c06 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -3894,6 +3894,65 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
).inferred()[0]
assert isinstance(cls, nodes.ClassDef) and cls.name == "Clazz"
+ def test_infer_subclass_attr_outer_class(self):
+ node = extract_node(
+ """
+ class Outer:
+ data = 123
+
+ class Test(Outer):
+ pass
+ Test.data
+ """
+ )
+ inferred = next(node.infer())
+ assert isinstance(inferred, nodes.Const)
+ assert inferred.value == 123
+
+ def test_infer_subclass_attr_inner_class_works_indirectly(self):
+ node = extract_node(
+ """
+ class Outer:
+ class Inner:
+ data = 123
+ Inner = Outer.Inner
+
+ class Test(Inner):
+ pass
+ Test.data
+ """
+ )
+ inferred = next(node.infer())
+ assert isinstance(inferred, nodes.Const)
+ assert inferred.value == 123
+
+ def test_infer_subclass_attr_inner_class(self):
+ clsdef_node, attr_node = extract_node(
+ """
+ class Outer:
+ class Inner:
+ data = 123
+
+ class Test(Outer.Inner):
+ pass
+ Test #@
+ Test.data #@
+ """
+ )
+ clsdef = next(clsdef_node.infer())
+ assert isinstance(clsdef, nodes.ClassDef)
+ inferred = next(clsdef.igetattr("data"))
+ assert isinstance(inferred, nodes.Const)
+ assert inferred.value == 123
+ # Inferring the value of .data via igetattr() worked before the
+ # old_boundnode fixes in infer_subscript, so it should have been
+ # possible to infer the subscript directly. It is the difference
+ # between these two cases that led to the discovery of the cause of the
+ # bug in https://github.com/PyCQA/astroid/issues/904
+ inferred = next(attr_node.infer())
+ assert isinstance(inferred, nodes.Const)
+ assert inferred.value == 123
+
def test_delayed_attributes_without_slots(self):
ast_node = extract_node(
"""