isinstance checks now also give you type hints in class contexts, fixes #241.

This commit is contained in:
Dave Halter
2014-08-04 02:11:30 +02:00
parent 0a0673e87c
commit 625e88e851
2 changed files with 16 additions and 11 deletions

View File

@@ -197,16 +197,19 @@ class NameFinder(object):
def _names_to_types(self, names, resolve_decorator): def _names_to_types(self, names, resolve_decorator):
types = [] types = []
evaluator = self._evaluator
# Add isinstance and other if/assert knowledge. # Add isinstance and other if/assert knowledge.
flow_scope = self.scope flow_scope = self.scope
evaluator = self._evaluator if isinstance(self.name_str, pr.NamePart):
while flow_scope: flow_scope = self.name_str.parent.parent
# TODO check if result is in scope -> no evaluation necessary while flow_scope:
n = check_flow_information(evaluator, flow_scope, # TODO check if result is in scope -> no evaluation necessary
self.name_str, self.position) n = check_flow_information(evaluator, flow_scope,
if n: self.name_str, self.position)
return n if n:
flow_scope = flow_scope.parent return n
flow_scope = flow_scope.parent
for name in names: for name in names:
typ = name.parent typ = name.parent
@@ -359,7 +362,7 @@ def check_flow_information(evaluator, flow, search_name_part, pos):
return None return None
result = [] result = []
if isinstance(flow, pr.IsScope) and not result: if isinstance(flow, pr.IsScope):
for ass in reversed(flow.asserts): for ass in reversed(flow.asserts):
if pos is None or ass.start_pos > pos: if pos is None or ass.start_pos > pos:
continue continue
@@ -391,7 +394,7 @@ def _check_isinstance_type(evaluator, stmt, search_name_part):
assert isinstance(obj[0], pr.Call) assert isinstance(obj[0], pr.Call)
# names fit? # names fit?
assert unicode(obj[0].name) == unicode(search_name_part) assert unicode(obj[0].name) == unicode(search_name_part.parent)
assert isinstance(classes[0], pr.StatementElement) # can be type or tuple assert isinstance(classes[0], pr.StatementElement) # can be type or tuple
except AssertionError: except AssertionError:
return [] return []

View File

@@ -65,5 +65,7 @@ class Test():
def boo(self): def boo(self):
if isinstance(self.testing, str): if isinstance(self.testing, str):
##? str() #? str()
self.testing self.testing
#? Test()
self