Add support for PEP 0526.

This makes it possible to assign variables like

    asdf: typing.List[int] = []
This commit is contained in:
Dave Halter
2017-01-08 03:57:35 +01:00
parent 6d00a5702f
commit 3f09f3a304
3 changed files with 36 additions and 4 deletions

View File

@@ -77,6 +77,7 @@ from jedi.evaluate import compiled
from jedi.evaluate import precedence from jedi.evaluate import precedence
from jedi.evaluate import param from jedi.evaluate import param
from jedi.evaluate import helpers from jedi.evaluate import helpers
from jedi.evaluate import pep0484
from jedi.evaluate.filters import TreeNameDefinition, ParamName from jedi.evaluate.filters import TreeNameDefinition, ParamName
from jedi.evaluate.instance import AnonymousInstance, BoundMethod from jedi.evaluate.instance import AnonymousInstance, BoundMethod
@@ -152,7 +153,7 @@ class Evaluator(object):
types = finder.check_tuple_assignments(self, types, seek_name) types = finder.check_tuple_assignments(self, types, seek_name)
first_operation = stmt.first_operation() first_operation = stmt.first_operation()
if first_operation not in ('=', None): if first_operation not in ('=', None) and first_operation.type == 'operator':
# `=` is always the last character in aug assignments -> -1 # `=` is always the last character in aug assignments -> -1
operator = copy.copy(first_operation) operator = copy.copy(first_operation)
operator.value = operator.value[:-1] operator.value = operator.value[:-1]
@@ -315,6 +316,10 @@ class Evaluator(object):
types = types types = types
elif element.type == 'eval_input': elif element.type == 'eval_input':
types = self._eval_element_not_cached(context, element.children[0]) types = self._eval_element_not_cached(context, element.children[0])
elif element.type == 'annassign':
print(element.children[1])
types = pep0484._evaluate_for_annotation(context, element.children[1])
print('xxx')
else: else:
types = precedence.calculate_children(self, context, element.children) types = precedence.calculate_children(self, context, element.children)
debug.dbg('eval_element result %s', types) debug.dbg('eval_element result %s', types)

View File

@@ -1531,9 +1531,14 @@ class ExprStmt(BaseNode, DocstringMixin):
__slots__ = () __slots__ = ()
def get_defined_names(self): def get_defined_names(self):
return list(chain.from_iterable(_defined_names(self.children[i]) names = []
if self.children[1].type == 'annassign':
names = _defined_names(self.children[0])
return list(chain.from_iterable(
_defined_names(self.children[i])
for i in range(0, len(self.children) - 2, 2) for i in range(0, len(self.children) - 2, 2)
if '=' in self.children[i + 1].value)) if '=' in self.children[i + 1].value)
) + names
def get_rhs(self): def get_rhs(self):
"""Returns the right-hand-side of the equals.""" """Returns the right-hand-side of the equals."""

View File

@@ -0,0 +1,22 @@
"""
PEP 526 introduced a new way of using type annotations on variables. It was
introduced in Python 3.6.
"""
# python >= 3.6
import typing
asdf = ''
asdf: int
# This is not necessarily correct, but for now this is ok (at least no error).
#? int()
asdf
direct: int = NOT_DEFINED
#? int()
direct
with_typing_module: typing.List[float] = NOT_DEFINED
#? float()
with_typing_module[0]