diff options
author | Bryce Guinta <bryce.paul.guinta@gmail.com> | 2018-03-28 20:19:49 -0600 |
---|---|---|
committer | Bryce Guinta <bryce.paul.guinta@gmail.com> | 2018-03-30 21:45:07 -0700 |
commit | f7f80522bdaad77e6a995384014ce2a342afec87 (patch) | |
tree | 6f6292b12d5eaab5c8850eb1ebe737361ed18840 /astroid/inference.py | |
parent | 045c05ef904e48124700958f214a0287ceb8e6ef (diff) | |
download | astroid-git-f7f80522bdaad77e6a995384014ce2a342afec87.tar.gz |
Add context_lookup to infer_call_result for function arguments
This allows inference of function arguments to be different than the
inference of the function body
Currently this is only used for object.__new__(cls) calls but
can be expanded later
Diffstat (limited to 'astroid/inference.py')
-rw-r--r-- | astroid/inference.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index 37b1b2bb..0a55b6e8 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -187,13 +187,15 @@ def infer_call(self, context=None): callcontext.callcontext = contextmod.CallContext(args=self.args, keywords=self.keywords) callcontext.boundnode = None + if context is not None: + context_lookup = _populate_context_lookup(self, context.clone()) for callee in self.func.infer(context): if callee is util.Uninferable: yield callee continue try: if hasattr(callee, 'infer_call_result'): - for inferred in callee.infer_call_result(self, callcontext): + for inferred in callee.infer_call_result(self, callcontext, context_lookup): yield inferred except exceptions.InferenceError: ## XXX log error ? @@ -832,3 +834,20 @@ def instance_getitem(self, index, context=None): node=self, index=index, context=context)) bases.Instance.getitem = instance_getitem + + +def _populate_context_lookup(call, context): + # Allows context to be saved for later + # for inference inside a function + context_lookup = {} + if context is None: + return context_lookup + for arg in call.args: + if isinstance(arg, nodes.Starred): + context_lookup[arg.value] = context + else: + context_lookup[arg] = context + keywords = call.keywords if call.keywords is not None else [] + for keyword in keywords: + context_lookup[keyword.value] = context + return context_lookup |