From 045787fd104032780dcb47445d79035c1f45f08a Mon Sep 17 00:00:00 2001 From: Bryce Guinta Date: Tue, 3 Jul 2018 17:50:43 -0600 Subject: Refactor assinging boundnode to context Code in multiple places had the same pattern for infering call results. --- astroid/bases.py | 12 ++---------- astroid/context.py | 25 +++++++++++++++++++++++++ astroid/inference.py | 11 ++++------- astroid/scoped_nodes.py | 13 ++----------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/astroid/bases.py b/astroid/bases.py index 4c79b65d..800e048c 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -228,11 +228,7 @@ class BaseInstance(Proxy): def infer_call_result(self, caller, context=None, context_lookup=None): """infer what a class instance is returning when called""" - if context is None: - context = contextmod.InferenceContext() - else: - context = context.clone() - context.boundnode = self + context = contextmod.bind_context_to_node(context, self) inferred = False for node in self._proxied.igetattr('__call__', context): if node is util.Uninferable or not node.callable(): @@ -447,11 +443,7 @@ class BoundMethod(UnboundMethod): return cls def infer_call_result(self, caller, context=None, context_lookup=None): - if context is None: - context = contextmod.InferenceContext() - context = context.clone() - context.boundnode = self.bound - + context = contextmod.bind_context_to_node(context, self.bound) if (self.bound.__class__.__name__ == 'ClassDef' and self.bound.name == 'type' and self.name == '__new__' diff --git a/astroid/context.py b/astroid/context.py index 3461fb2b..75cab636 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -136,3 +136,28 @@ def copy_context(context): return context.clone() return InferenceContext() + + +def bind_context_to_node(context, node): + """Give a context a boundnode + to retrieve the correct function name or attribute value + with from further inference. + + Do not use an existing context since the boundnode could then + be incorrectly propagated higher up in the call stack. + + :param context: Context to use + :type context: Optional(context) + + :param node: Node to do name lookups from + :type node NodeNG: + + :returns: A new context + :rtype: InferenceContext + """ + if context is not None: + context = context.clone() + else: + context = InferenceContext() + context.boundnode = node + return context diff --git a/astroid/inference.py b/astroid/inference.py index 33730a39..b9e098b4 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -537,8 +537,7 @@ def _is_not_implemented(const): def _invoke_binop_inference(instance, opnode, op, other, context, method_name): """Invoke binary operation inference on the given instance.""" methods = dunder_lookup.lookup(instance, method_name) - if context is not None: - context.boundnode = instance + context = contextmod.bind_context_to_node(context, instance) method = methods[0] inferred = next(method.infer(context=context)) if inferred is util.Uninferable: @@ -824,14 +823,12 @@ nodes.Index._infer = infer_index # will be solved. def instance_getitem(self, index, context=None): # Rewrap index to Const for this case - if context: - new_context = context.clone() - else: - context = new_context = contextmod.InferenceContext() + new_context = contextmod.bind_context_to_node(context, self) + if not context: + context = new_context # Create a new callcontext for providing index as an argument. new_context.callcontext = contextmod.CallContext(args=[index]) - new_context.boundnode = self method = next(self.igetattr('__getitem__', context=context), None) if not isinstance(method, bases.BoundMethod): diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index b7b65857..dd9cfd44 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -2047,11 +2047,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, pass if (dunder_call is not None and dunder_call.qname() != "builtins.type.__call__"): - if context is not None: - context = context.clone() - else: - context = contextmod.InferenceContext() - context.boundnode = self + context = contextmod.bind_context_to_node(context, self) yield from dunder_call.infer_call_result( caller, context, context_lookup) else: @@ -2440,13 +2436,8 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, method = methods[0] # Create a new callcontext for providing index as an argument. - if context: - new_context = context.clone() - else: - new_context = contextmod.InferenceContext() - + new_context = contextmod.bind_context_to_node(context, self) new_context.callcontext = contextmod.CallContext(args=[index]) - new_context.boundnode = self return next(method.infer_call_result(self, new_context)) -- cgit v1.2.1