1
0
forked from VimPlug/jedi

flow analysis working for elif statements (even in combination with else)

This commit is contained in:
Dave Halter
2014-08-07 12:15:25 +02:00
parent 743d064e6d
commit 0ae9e520c1
2 changed files with 86 additions and 19 deletions

View File

@@ -35,26 +35,33 @@ UNSURE = Status(None, 'unsure')
def break_check(evaluator, base_scope, element_scope):
reachable = REACHABLE
if isinstance(element_scope, Flow):
check_scope = element_scope
invert = False
if check_scope.command == 'else':
check_scope = check_scope.previous
invert = True
if element_scope.command == 'else':
check_scope = element_scope
while check_scope.previous is not None:
check_scope = check_scope.previous
reachable = _check_flow(evaluator, check_scope)
if reachable in (REACHABLE, UNSURE):
break
reachable = reachable.invert()
else:
reachable = _check_flow(evaluator, element_scope)
if check_scope.command == 'if' and check_scope.inputs:
types = evaluator.eval_statement(check_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 check_scope.command in ('try', 'except', 'finally'):
reachable = UNSURE
# Only reachable branches need to be examined further.
if reachable in (UNREACHABLE, UNSURE):
return reachable
if base_scope != element_scope and base_scope != check_scope.parent:
if base_scope != element_scope and base_scope != element_scope.parent:
return reachable & break_check(evaluator, base_scope, element_scope.parent)
return reachable
def _check_flow(evaluator, flow):
if flow.command in ('elif', 'if') and flow.inputs:
types = evaluator.eval_statement(flow.inputs[0])
values = set(x.py__bool__() for x in types)
if len(values) == 1:
return Status.lookup_table[values.pop()]
else:
return UNSURE
elif flow.command in ('try', 'except', 'finally', 'while'):
return UNSURE