forked from VimPlug/jedi
support tuple-assignment
This commit is contained in:
@@ -355,7 +355,7 @@ def _remove_statements(evaluator, stmt, name):
|
|||||||
check_instance = stmt.instance
|
check_instance = stmt.instance
|
||||||
stmt = stmt.var
|
stmt = stmt.var
|
||||||
|
|
||||||
pep0484types = pep0484.find_type_from_comment_hint(evaluator, stmt)
|
pep0484types = pep0484.find_type_from_comment_hint(evaluator, stmt, name)
|
||||||
if pep0484types:
|
if pep0484types:
|
||||||
return pep0484types
|
return pep0484types
|
||||||
types |= evaluator.eval_statement(stmt, seek_name=name)
|
types |= evaluator.eval_statement(stmt, seek_name=name)
|
||||||
|
|||||||
@@ -31,10 +31,20 @@ from jedi import _compatibility
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def _evaluate_for_annotation(evaluator, annotation):
|
def _evaluate_for_annotation(evaluator, annotation, index=None):
|
||||||
|
"""
|
||||||
|
Evaluates a string-node, looking for an annotation
|
||||||
|
If index is not None, the annotation is expected to be a tuple
|
||||||
|
and we're interested in that index
|
||||||
|
"""
|
||||||
if annotation is not None:
|
if annotation is not None:
|
||||||
definitions = evaluator.eval_element(
|
definitions = evaluator.eval_element(
|
||||||
_fix_forward_reference(evaluator, annotation))
|
_fix_forward_reference(evaluator, annotation))
|
||||||
|
if index is not None:
|
||||||
|
definitions = list(itertools.chain.from_iterable(
|
||||||
|
definition.py__getitem__(index) for definition in definitions
|
||||||
|
if definition.type == 'tuple' and
|
||||||
|
len(list(definition.py__iter__())) >= index))
|
||||||
return list(itertools.chain.from_iterable(
|
return list(itertools.chain.from_iterable(
|
||||||
evaluator.execute(d) for d in definitions))
|
evaluator.execute(d) for d in definitions))
|
||||||
else:
|
else:
|
||||||
@@ -139,7 +149,21 @@ def get_types_for_typing_module(evaluator, typ, node):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def find_type_from_comment_hint(evaluator, stmt):
|
def find_type_from_comment_hint(evaluator, stmt, name):
|
||||||
|
index = None
|
||||||
|
if stmt.children[0].type == "testlist_star_expr":
|
||||||
|
# something like "a, b = 1, 2"
|
||||||
|
leftside = stmt.children[0]
|
||||||
|
index = 0
|
||||||
|
for child in leftside.children:
|
||||||
|
if child == name:
|
||||||
|
break
|
||||||
|
if child.type == "operator":
|
||||||
|
continue
|
||||||
|
index += 1
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
comment = stmt.get_following_comment_same_line()
|
comment = stmt.get_following_comment_same_line()
|
||||||
if comment is None:
|
if comment is None:
|
||||||
return []
|
return []
|
||||||
@@ -151,4 +175,4 @@ def find_type_from_comment_hint(evaluator, stmt):
|
|||||||
repr(str(match.group(1).strip())),
|
repr(str(match.group(1).strip())),
|
||||||
stmt.start_pos)
|
stmt.start_pos)
|
||||||
annotation.parent = stmt.parent
|
annotation.parent = stmt.parent
|
||||||
return _evaluate_for_annotation(evaluator, annotation)
|
return _evaluate_for_annotation(evaluator, annotation, index)
|
||||||
|
|||||||
+5
-1
@@ -514,7 +514,7 @@ class BaseNode(Base):
|
|||||||
|
|
||||||
def last_leaf(self):
|
def last_leaf(self):
|
||||||
try:
|
try:
|
||||||
return self.children[-1].first_leaf()
|
return self.children[-1].last_leaf()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return self.children[-1]
|
return self.children[-1]
|
||||||
|
|
||||||
@@ -527,6 +527,10 @@ class BaseNode(Base):
|
|||||||
whitespace = self.last_leaf().get_next().prefix
|
whitespace = self.last_leaf().get_next().prefix
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return None
|
return None
|
||||||
|
except ValueError:
|
||||||
|
# in some particular cases, the tree doesn't seem to be linked
|
||||||
|
# correctly
|
||||||
|
return None
|
||||||
if "#" not in whitespace:
|
if "#" not in whitespace:
|
||||||
return None
|
return None
|
||||||
comment = whitespace[whitespace.index("#"):]
|
comment = whitespace[whitespace.index("#"):]
|
||||||
|
|||||||
@@ -37,3 +37,32 @@ def test(a, b):
|
|||||||
d
|
d
|
||||||
#? str()
|
#? str()
|
||||||
e
|
e
|
||||||
|
|
||||||
|
a,b = 1, 2 # type: str, float
|
||||||
|
#? str()
|
||||||
|
a
|
||||||
|
#? float()
|
||||||
|
b
|
||||||
|
|
||||||
|
class Employee:
|
||||||
|
pass
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
x = [] # type: List[Employee]
|
||||||
|
#? Employee()
|
||||||
|
x[1]
|
||||||
|
x, y, z = [], [], [] # type: List[int], List[int], List[str]
|
||||||
|
#? int()
|
||||||
|
y[2]
|
||||||
|
x, y, z = [], [], [] # type: (List[float], List[float], List[BB])
|
||||||
|
for zi in z:
|
||||||
|
#? BB()
|
||||||
|
zi
|
||||||
|
|
||||||
|
x = [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
] # type: List[str]
|
||||||
|
|
||||||
|
#? str()
|
||||||
|
x[1]
|
||||||
|
|||||||
Reference in New Issue
Block a user