Skip to content

Commit

Permalink
Add support for PEP 0526.
Browse files Browse the repository at this point in the history
This makes it possible to assign variables like

    asdf: typing.List[int] = []
  • Loading branch information
davidhalter committed Jan 8, 2017
1 parent 6d00a57 commit 3f09f3a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
7 changes: 6 additions & 1 deletion jedi/evaluate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
from jedi.evaluate import precedence
from jedi.evaluate import param
from jedi.evaluate import helpers
from jedi.evaluate import pep0484
from jedi.evaluate.filters import TreeNameDefinition, ParamName
from jedi.evaluate.instance import AnonymousInstance, BoundMethod

Expand Down Expand Up @@ -152,7 +153,7 @@ def _eval_stmt(self, context, stmt, seek_name=None):
types = finder.check_tuple_assignments(self, types, seek_name)

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
operator = copy.copy(first_operation)
operator.value = operator.value[:-1]
Expand Down Expand Up @@ -315,6 +316,10 @@ def _eval_element_not_cached(self, context, element):
types = types
elif element.type == 'eval_input':
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:
types = precedence.calculate_children(self, context, element.children)
debug.dbg('eval_element result %s', types)
Expand Down
11 changes: 8 additions & 3 deletions jedi/parser/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,9 +1531,14 @@ class ExprStmt(BaseNode, DocstringMixin):
__slots__ = ()

def get_defined_names(self):
return list(chain.from_iterable(_defined_names(self.children[i])
for i in range(0, len(self.children) - 2, 2)
if '=' in self.children[i + 1].value))
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)
if '=' in self.children[i + 1].value)
) + names

def get_rhs(self):
"""Returns the right-hand-side of the equals."""
Expand Down
22 changes: 22 additions & 0 deletions test/completion/pep0526_variables.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 3f09f3a

Please sign in to comment.