summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-10-23 13:57:25 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2015-10-23 13:57:25 +0100
commitb50e93e14255cb63a60623e214384ecf78dcc483 (patch)
tree43d84162fa0e2251e434e66239776e86a7c5bd1c
parent2959dd8c26060d06593e50241b2747ffd3c4d6bb (diff)
parent6b2f28cf6f4d83112a60946d6dcaa0d889dd516e (diff)
downloadastroid-b50e93e14255cb63a60623e214384ecf78dcc483.tar.gz
Merged in dmand/astroid/binop-inference-fix (pull request #92)
Fix binary operator inference issue from pylint's #646
-rw-r--r--astroid/inference.py14
-rw-r--r--astroid/tests/unittest_inference.py12
2 files changed, 19 insertions, 7 deletions
diff --git a/astroid/inference.py b/astroid/inference.py
index 3253acb..2943596 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -582,18 +582,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.
diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py
index e3cade4..bce1db4 100644
--- a/astroid/tests/unittest_inference.py
+++ b/astroid/tests/unittest_inference.py
@@ -1018,6 +1018,18 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertEqual(len(inferred), 1)
self.assertEqual(inferred[0], util.YES)
+ def test_binary_op_on_self(self):
+ 'test correct handling of applying binary operator to self'
+ code = '''
+ import sys
+ sys.path = ['foo'] + sys.path
+ sys.path.insert(0, 'bar')
+ path = sys.path
+ '''
+ ast = parse(code, __name__)
+ inferred = ast['path'].inferred()
+ self.assertIsInstance(inferred[0], nodes.List)
+
def test_binary_op_tuple_add(self):
ast = builder.string_build('a = (1,) + (2,)', __name__, __file__)
inferred = list(ast['a'].infer())