diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-06-01 11:30:10 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-06-01 11:30:10 +0200 |
commit | 118c9caf1f0dee8785a815bec203ee8e97ef75b0 (patch) | |
tree | 6a9a88a79810dc1343ea8ec6e884e8eddabbb3de /astroid/inference.py | |
parent | a1a2655504ae023177444a15da500b7e4fa340cd (diff) | |
download | astroid-git-118c9caf1f0dee8785a815bec203ee8e97ef75b0.tar.gz |
Added support for inferring `IfExp` nodes.
Diffstat (limited to 'astroid/inference.py')
-rw-r--r-- | astroid/inference.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index 16346a77..1ef5849f 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -897,3 +897,32 @@ def _populate_context_lookup(call, context): for keyword in keywords: context_lookup[keyword.value] = context return context_lookup + + +@decorators.raise_if_nothing_inferred +def infer_ifexp(self, context=None): + """Support IfExp inference + + If we can't infer the truthiness of the condition, we default + to inferring both branches. Otherwise, we infer either branch + depending on the condition. + """ + both_branches = False + try: + test = next(self.test.infer(context=context)) + except exceptions.InferenceError: + both_branches = True + else: + if test is not util.Uninferable: + if test.bool_value(): + yield from self.body.infer(context=context) + else: + yield from self.orelse.infer(context=context) + else: + both_branches = True + if both_branches: + yield from self.body.infer(context=context) + yield from self.orelse.infer(context=context) + + +nodes.IfExp._infer = infer_ifexp |