diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-10-22 20:52:58 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-10-22 20:52:58 +0100 |
commit | 4920076bfe852de098578034cfe56e8ef0ef96bf (patch) | |
tree | a28aa6cc4b52faf35f3126a2d8eab94ebe38ed2b /astroid | |
parent | 658ecfbb170c040e079ce5619d751fcc3510d4b7 (diff) | |
download | astroid-4920076bfe852de098578034cfe56e8ef0ef96bf.tar.gz |
Create a call context when inferring unary operands on instances.
Diffstat (limited to 'astroid')
-rw-r--r-- | astroid/inference.py | 3 | ||||
-rw-r--r-- | astroid/tests/unittest_inference.py | 31 |
2 files changed, 19 insertions, 15 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index 010607c..b1c81da 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -372,6 +372,9 @@ def _infer_unaryop(self, context=None): inferred = next(meth.infer(context=context)) if inferred is util.YES or not inferred.callable(): continue + + context = contextmod.copy_context(context) + context.callcontext = contextmod.CallContext(args=[operand]) call_results = inferred.infer_call_result(self, context=context) result = next(call_results, None) if result is None: diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index d7c1ac4..02e9fcd 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2004,17 +2004,25 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def __neg__(self): return +self - 41 def __invert__(self): - return [] + return 42 class BadInstance(object): def __pos__(self): return lala def __neg__(self): return missing + class LambdaInstance(object): + __pos__ = lambda self: self.lala + __neg__ = lambda self: self.lala + 1 + @property + def lala(self): return 24 instance = GoodInstance() + lambda_instance = LambdaInstance() +instance #@ -instance #@ ~instance #@ --instance #@ + +lambda_instance #@ + -lambda_instance #@ bad_instance = BadInstance() +bad_instance #@ @@ -2027,20 +2035,13 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): -func #@ +BadInstance #@ ''') - pos = next(ast_nodes[0].infer()) - self.assertIsInstance(pos, nodes.Const) - self.assertEqual(pos.value, 42) - neg = next(ast_nodes[1].infer()) - self.assertIsInstance(neg, nodes.Const) - self.assertEqual(neg.value, 1) - invert = next(ast_nodes[2].infer()) - self.assertIsInstance(invert, nodes.List) - self.assertEqual(invert.elts, []) - neg_neg = next(ast_nodes[3].infer()) - self.assertIsInstance(neg_neg, nodes.Const) - self.assertEqual(neg_neg.value, -1) - - for bad_node in ast_nodes[4:]: + expected = [42, 1, 42, -1, 24, 25] + for node, value in zip(ast_nodes[:6], expected): + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, value) + + for bad_node in ast_nodes[6:]: inferred = next(bad_node.infer()) self.assertEqual(inferred, util.YES) |