summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Pribysh <dmand@yandex.ru>2015-10-21 16:16:01 +0300
committerDmitry Pribysh <dmand@yandex.ru>2015-10-21 16:16:01 +0300
commit6b2f28cf6f4d83112a60946d6dcaa0d889dd516e (patch)
tree3891809e885836660e6b86f989bc21cecb5ee9fd
parentfba7aa120b77390edb189a9316d5fb4556886f61 (diff)
downloadastroid-binop-inference-fix.tar.gz
Fix binary operation inference crash by using cloned contextsbinop-inference-fix
Idea is that we shouldn't use the same context to infer left-hand side and right-hand side of the binary operation because inferring lhs may leave some metadata in context.path that is irrelevant (and can even break inference) to inference of rhs. So we make two clones of the original context and use them to infer left-hand side and right-hand side independently. And then we use the original context to infer the result of the operation applied to inferred values. This patch fixes pylint's issue #646 and may be also related to other similar astroid issues (#198, #199).
-rw-r--r--astroid/inference.py14
1 files changed, 7 insertions, 7 deletions
diff --git a/astroid/inference.py b/astroid/inference.py
index 010607c..16d5311 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -575,18 +575,18 @@ def _infer_binop(self, context):
right = self.right
op = self.op
- for lhs in left.infer(context=context):
+ # we use two separate contexts for evaluating lhs and rhs because
+ # 1. evaluating lhs may leave some undesired entries in context.path
+ # which may not let us infer right value of rhs
+ lhs_context = context.clone()
+ rhs_context = context.clone()
+
+ for lhs in left.infer(context=lhs_context):
if lhs is util.YES:
# Don't know how to process this.
yield util.YES
return
- # TODO(cpopa): if we have A() * A(), trying to infer
- # the rhs with the same context will result in an
- # inferrence error, so we create another context for it.
- # This is a bug which should be fixed in InferenceContext at some point.
- rhs_context = context.clone()
- rhs_context.path = set()
for rhs in right.infer(context=rhs_context):
if rhs is util.YES:
# Don't know how to process this.