1
0
forked from VimPlug/jedi

forward reference pep-0484

This commit is contained in:
Claude
2015-12-13 23:47:45 +01:00
parent c61f39cb2b
commit f8debace0d
3 changed files with 42 additions and 4 deletions

View File

@@ -7,8 +7,8 @@ as annotations in future python versions.
The (initial / probably incomplete) implementation todo list for pep-0484:
v Function parameter annotations with builtin/custom type classes
v Function returntype annotations with builtin/custom type classes
x Function parameter annotations with strings (forward reference)
x Function return type annotations with strings (forward reference)
v Function parameter annotations with strings (forward reference)
v Function return type annotations with strings (forward reference)
x Local variable type hints
x Assigned types: `Url = str\ndef get(url:Url) -> str:`
x Type hints in `with` statements

View File

@@ -41,6 +41,7 @@ import textwrap
from jedi._compatibility import (Python3Method, encoding, is_py3, utf8_repr,
literal_eval, use_metaclass, unicode)
from jedi import cache
import ast
def is_node(node, *symbol_names):
@@ -52,6 +53,19 @@ def is_node(node, *symbol_names):
return type in symbol_names
def _fix_forward_reference(annotation):
if isinstance(annotation, String):
newannotation = Name(
annotation.position_modifier,
ast.literal_eval(annotation.value),
annotation.start_pos,
annotation.prefix)
newannotation.parent = annotation.parent
else:
newannotation = annotation
return newannotation
class PositionModifier(object):
"""A start_pos modifier for the fast parser."""
def __init__(self):
@@ -865,9 +879,12 @@ class Function(ClassOrFunc):
return bool(self.yields)
def annotation(self):
if self.children[0] == "lambda":
# lambda functions have no annotation
return None
try:
if self.children[3] == "->":
return self.children[4]
return _fix_forward_reference(self.children[4])
assert self.children[3] == ":"
except IndexError:
return None
@@ -1409,7 +1426,8 @@ class Param(BaseNode):
if is_node(tfpdef, 'tfpdef'):
assert tfpdef.children[1] == ":"
assert len(tfpdef.children) == 3
return tfpdef.children[2]
annotation = tfpdef.children[2]
return _fix_forward_reference(annotation)
else:
return None

View File

@@ -68,3 +68,23 @@ def return_annotation_and_docstring_different() -> str:
#? str()
return_annotation_and_docstring_different()
def annotation_forward_reference(b: "B") -> "B":
#? B()
b
#? B()
annotation_forward_reference(1)
class B:
pass
class SelfReference:
def test(x: "SelfReference") -> "SelfReference":
#? SelfReference()
x
#? SelfReference()
SelfReference().test()