diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2016-12-30 15:57:15 +0200 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-12-30 15:57:15 +0200 |
| commit | 2fca596fa72783daf315855b4877d1a94b984e0e (patch) | |
| tree | 6abea649fb45b5cff335d594d7f7e0c06408ce88 | |
| parent | e37acdc4bfe7e0d714e86a5c7f5d9f956cef15e5 (diff) | |
| download | astroid-git-2fca596fa72783daf315855b4877d1a94b984e0e.tar.gz | |
Add support for binary operations between classes.
| -rw-r--r-- | astroid/inference.py | 9 | ||||
| -rw-r--r-- | astroid/protocols.py | 4 | ||||
| -rw-r--r-- | astroid/tests/unittest_inference.py | 16 |
3 files changed, 26 insertions, 3 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index 4e46b4e1..112ce986 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -131,7 +131,7 @@ def infer_import(self, context=None, asname=True): yield inferenceutil.do_import_module(self, name) except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, - context=context)) + context=context)) @infer.register(treeabc.ImportFrom) @@ -424,7 +424,12 @@ def _invoke_binop_inference(instance, opnode, op, other, context, method_name, # the inference at this point. raise exceptions.BinaryOperationNotSupportedError - method = instance.getattr(method_name)[0] + try: + methods = dunder_lookup.lookup(instance, method_name) + except exceptions.NotSupportedError: + raise exceptions.BinaryOperationNotSupportedError + + method = methods[0] inferred = next(method.infer(context=context)) return protocols.infer_binary_op(instance, opnode, op, other, context, inferred, nodes) diff --git a/astroid/protocols.py b/astroid/protocols.py index baf3762e..cd1bec7e 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -217,8 +217,10 @@ def _get_binop_context(context, other): @infer_binary_op.register(runtimeabc.Instance) +@infer_binary_op.register(treeabc.ClassDef) @decorators.yes_if_nothing_inferred -def instance_infer_binary_op(self, opnode, operator, other, context, method, nodes): +def instance_class_infer_binary_op( + self, opnode, operator, other, context, method, nodes): context = _get_binop_context(context, other) return method.infer_call_result(self, context) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 4c13e841..9579850f 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -2537,6 +2537,22 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): for node in ast_nodes: self.assertEqual(next(node.infer()), util.Uninferable) + def test_bin_op_classes(self): + ast_node = extract_node(''' + class Meta(type): + def __or__(self, other): + return 24 + import six + @six.add_metaclass(Meta) + class A(object): + pass + + A | A + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 24) + def test_bin_op_supertype_more_complicated_example(self): ast_node = extract_node(''' class A(object): |
