summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-10-18 10:50:35 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2019-10-18 10:50:35 +0200
commited42e0ceea0d2b1eb9cbac3562df28a185c85aaf (patch)
treeb6b852d16d81cafb30c1d855a63d99b907671cd3
parent0088cc0f8eabd6e1d8a8159396218e120a4dfeaf (diff)
downloadastroid-git-ed42e0ceea0d2b1eb9cbac3562df28a185c85aaf.tar.gz
Pass an inference context to `metaclass()` when inferring an object type
This should prevent a bunch of recursion errors happening in pylint. Also refactor the inference of `IfExp` nodes to use separate contexts for each potential branch. Close PyCQA/pylint#3152 Close PyCQA/pylint#3159
-rw-r--r--ChangeLog10
-rw-r--r--astroid/helpers.py2
-rw-r--r--astroid/inference.py17
3 files changed, 23 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 35688bd3..0d13dcda 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,16 @@ Release Date: TBA
Close PyCQA/pylint#3174
+* Pass an inference context to `metaclass()` when inferring an object type
+
+ This should prevent a bunch of recursion errors happening in pylint.
+ Also refactor the inference of `IfExp` nodes to use separate contexts
+ for each potential branch.
+
+ Close PyCQA/pylint#3152
+ Close PyCQA/pylint#3159
+
+
What's New in astroid 2.3.1?
============================
Release Date: 2019-09-30
diff --git a/astroid/helpers.py b/astroid/helpers.py
index 942b0a02..cdfe6c09 100644
--- a/astroid/helpers.py
+++ b/astroid/helpers.py
@@ -52,7 +52,7 @@ def _object_type(node, context=None):
for inferred in node.infer(context=context):
if isinstance(inferred, scoped_nodes.ClassDef):
if inferred.newstyle:
- metaclass = inferred.metaclass()
+ metaclass = inferred.metaclass(context=context)
if metaclass:
yield metaclass
continue
diff --git a/astroid/inference.py b/astroid/inference.py
index 1ea45248..77c6b1d0 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -916,21 +916,28 @@ def infer_ifexp(self, context=None):
depending on the condition.
"""
both_branches = False
+ # We use two separate contexts for evaluating lhs and rhs because
+ # evaluating lhs may leave some undesired entries in context.path
+ # which may not let us infer right value of rhs.
+
+ context = context or contextmod.InferenceContext()
+ lhs_context = contextmod.copy_context(context)
+ rhs_context = contextmod.copy_context(context)
try:
- test = next(self.test.infer(context=context))
+ test = next(self.test.infer(context=context.clone()))
except exceptions.InferenceError:
both_branches = True
else:
if test is not util.Uninferable:
if test.bool_value():
- yield from self.body.infer(context=context)
+ yield from self.body.infer(context=lhs_context)
else:
- yield from self.orelse.infer(context=context)
+ yield from self.orelse.infer(context=rhs_context)
else:
both_branches = True
if both_branches:
- yield from self.body.infer(context=context)
- yield from self.orelse.infer(context=context)
+ yield from self.body.infer(context=lhs_context)
+ yield from self.orelse.infer(context=rhs_context)
nodes.IfExp._infer = infer_ifexp