From ee65764c3aa131155591db313edd3828c8414cc8 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 7 Aug 2014 12:02:08 +0200 Subject: [PATCH] more complicated logic working with else --- jedi/evaluate/flow_analysis.py | 17 +++++++++++++++++ test/completion/flow_analysis.py | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/jedi/evaluate/flow_analysis.py b/jedi/evaluate/flow_analysis.py index ce4c21df..01c29529 100644 --- a/jedi/evaluate/flow_analysis.py +++ b/jedi/evaluate/flow_analysis.py @@ -9,6 +9,14 @@ class Status(object): self._name = name Status.lookup_table[value] = self + def invert(self): + if self is REACHABLE: + return UNREACHABLE + elif self is UNREACHABLE: + return REACHABLE + else: + return UNSURE + def __and__(self, other): if UNSURE in (self, other): return UNSURE @@ -27,11 +35,20 @@ UNSURE = Status(None, 'unsure') def break_check(evaluator, base_scope, element_scope): reachable = REACHABLE if isinstance(element_scope, Flow): + invert = False + if element_scope.command == 'else': + element_scope = element_scope.previous + invert = True + if element_scope.command == 'if' and element_scope.inputs: types = evaluator.eval_statement(element_scope.inputs[0]) values = set(x.py__bool__() for x in types) if len(values) == 1: reachable = Status.lookup_table[values.pop()] + if invert: + reachable = reachable.invert() + if reachable is UNREACHABLE: + return UNREACHABLE else: return UNSURE elif element_scope.command == 'try': diff --git a/test/completion/flow_analysis.py b/test/completion/flow_analysis.py index 17f1e272..75f7b4e9 100644 --- a/test/completion/flow_analysis.py +++ b/test/completion/flow_analysis.py @@ -6,3 +6,17 @@ def foo(x): #? int() foo(1) + + +# Exceptions are not analyzed. So check both if branches +def try_except(x): + try: + if 1.0: + return 1 + else: + return '' + except AttributeError: + return 1.0 + +#? int() float() +try_except(1)