summaryrefslogtreecommitdiff
path: root/tests
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 /tests
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
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_inference.py59
1 files changed, 59 insertions, 0 deletions
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(
"""