mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
isinstance checks now also give you type hints in class contexts, fixes #241.
This commit is contained in:
@@ -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 []
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user