1
0
forked from VimPlug/jedi

isinstance checks for if/while

This commit is contained in:
David Halter
2012-10-04 00:34:26 +02:00
parent 8d842dd596
commit 7e39a7d1ba
4 changed files with 77 additions and 7 deletions

View File

@@ -492,3 +492,43 @@ class RelatedName(BaseOutput):
def __hash__(self):
return hash((self.start_pos, self.module_path))
def check_flow_information(flow, search_name):
""" Try to find out the type of a variable just with the information that
is given by the flows: e.g.
>>> if isinstance(k, str):
>>> k. # <- completion here
ensures that `k` is a string.
"""
try:
assert flow.command in ['if', 'while']
assert len(flow.inits) == 1
ass = flow.inits[0].get_assignment_calls()
assert len(ass.values) == 1 and len(ass.values[0]) == 1
call = ass.values[0][0]
assert type(call) == parsing.Call and str(call.name) == 'isinstance'
assert bool(call.execution)
# isinstance check
isinst = call.execution.values
assert len(isinst) == 2
assert len(isinst[0]) == 1
assert len(isinst[1]) == 1
assert isinstance(isinst[0][0], parsing.Call)
# names fit?
assert str(isinst[0][0].name) == search_name
classes_call = isinst[1][0] # class_or_type_or_tuple
assert isinstance(classes_call, parsing.Call)
result = []
for c in evaluate.follow_call(classes_call):
if isinstance(c, evaluate.Array):
result += c.get_index_types()
else:
result.append(c)
for i, c in enumerate(result):
result[i] = evaluate.Instance(c)
return result
except AssertionError:
return []