summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-03-10 10:51:25 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2020-03-10 10:51:25 +0100
commit8b80e9b07e0c3f9b34c5a99fba3286ee78c1fb75 (patch)
tree8fff80c9995722ddac5012459141fcad2943688c
parent77e205dc64ead82e38bb901e08117ef6f6162c5c (diff)
downloadastroid-git-8b80e9b07e0c3f9b34c5a99fba3286ee78c1fb75.tar.gz
Prevent a recursion error for self reference variables and `type()` calls.
The recursion error was caused by inferring the bases of the generated `B` class multiple times. The solution uses the new `EvaluatedObject` class to stop the inference by inferring the base classes in the `type` inference function. Close #199
-rw-r--r--ChangeLog4
-rw-r--r--astroid/scoped_nodes.py9
-rw-r--r--tests/unittest_inference.py20
3 files changed, 32 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 884e83b2..ce1753a0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -18,6 +18,10 @@ Release Date: TBA
Close PyCQA/pylint#2991
+* Prevent a recursion error for self reference variables and `type()` calls.
+
+ Close #199
+
* Do not infer the first argument of a staticmethod in a metaclass as the class itself
Close PyCQA/pylint#3032
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index 60c6499b..4364aa6b 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -2126,7 +2126,14 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement
# Get the bases of the class.
class_bases = next(caller.args[1].infer(context))
if isinstance(class_bases, (node_classes.Tuple, node_classes.List)):
- result.bases = class_bases.itered()
+ bases = []
+ for base in class_bases.itered():
+ inferred = next(base.infer(context=context))
+ if inferred:
+ bases.append(
+ node_classes.EvaluatedObject(original=base, value=inferred)
+ )
+ result.bases = bases
else:
# There is currently no AST node that can represent an 'unknown'
# node (Uninferable is not an AST node), therefore we simply return Uninferable here
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index dfae1b8f..6c6331d9 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -5779,5 +5779,25 @@ def test_recursion_error_metaclass_monkeypatching():
assert cls.declared_metaclass() is None
+@pytest.mark.xfail(reason="Cannot fully infer all the base classes properly.")
+def test_recursion_error_self_reference_type_call():
+ # Fix for https://github.com/PyCQA/astroid/issues/199
+ code = """
+ class A(object):
+ pass
+ class SomeClass(object):
+ route_class = A
+ def __init__(self):
+ self.route_class = type('B', (self.route_class, ), {})
+ self.route_class() #@
+ """
+ node = extract_node(code)
+ inferred = next(node.infer())
+ assert isinstance(inferred, Instance)
+ assert inferred.name == "B"
+ # TODO: Cannot infer [B, A, object] but at least the recursion error is gone.
+ assert [cls.name for cls in inferred.mro()] == ["B", "A", "object"]
+
+
if __name__ == "__main__":
unittest.main()