mirror of
https://github.com/davidhalter/jedi.git
synced 2026-03-05 04:24:16 +08:00
forward reference pep-0484
This commit is contained in:
@@ -7,8 +7,8 @@ as annotations in future python versions.
|
|||||||
The (initial / probably incomplete) implementation todo list for pep-0484:
|
The (initial / probably incomplete) implementation todo list for pep-0484:
|
||||||
v Function parameter annotations with builtin/custom type classes
|
v Function parameter annotations with builtin/custom type classes
|
||||||
v Function returntype annotations with builtin/custom type classes
|
v Function returntype annotations with builtin/custom type classes
|
||||||
x Function parameter annotations with strings (forward reference)
|
v Function parameter annotations with strings (forward reference)
|
||||||
x Function return type annotations with strings (forward reference)
|
v Function return type annotations with strings (forward reference)
|
||||||
x Local variable type hints
|
x Local variable type hints
|
||||||
x Assigned types: `Url = str\ndef get(url:Url) -> str:`
|
x Assigned types: `Url = str\ndef get(url:Url) -> str:`
|
||||||
x Type hints in `with` statements
|
x Type hints in `with` statements
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import textwrap
|
|||||||
from jedi._compatibility import (Python3Method, encoding, is_py3, utf8_repr,
|
from jedi._compatibility import (Python3Method, encoding, is_py3, utf8_repr,
|
||||||
literal_eval, use_metaclass, unicode)
|
literal_eval, use_metaclass, unicode)
|
||||||
from jedi import cache
|
from jedi import cache
|
||||||
|
import ast
|
||||||
|
|
||||||
|
|
||||||
def is_node(node, *symbol_names):
|
def is_node(node, *symbol_names):
|
||||||
@@ -52,6 +53,19 @@ def is_node(node, *symbol_names):
|
|||||||
return type in 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):
|
class PositionModifier(object):
|
||||||
"""A start_pos modifier for the fast parser."""
|
"""A start_pos modifier for the fast parser."""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -865,9 +879,12 @@ class Function(ClassOrFunc):
|
|||||||
return bool(self.yields)
|
return bool(self.yields)
|
||||||
|
|
||||||
def annotation(self):
|
def annotation(self):
|
||||||
|
if self.children[0] == "lambda":
|
||||||
|
# lambda functions have no annotation
|
||||||
|
return None
|
||||||
try:
|
try:
|
||||||
if self.children[3] == "->":
|
if self.children[3] == "->":
|
||||||
return self.children[4]
|
return _fix_forward_reference(self.children[4])
|
||||||
assert self.children[3] == ":"
|
assert self.children[3] == ":"
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
@@ -1409,7 +1426,8 @@ class Param(BaseNode):
|
|||||||
if is_node(tfpdef, 'tfpdef'):
|
if is_node(tfpdef, 'tfpdef'):
|
||||||
assert tfpdef.children[1] == ":"
|
assert tfpdef.children[1] == ":"
|
||||||
assert len(tfpdef.children) == 3
|
assert len(tfpdef.children) == 3
|
||||||
return tfpdef.children[2]
|
annotation = tfpdef.children[2]
|
||||||
|
return _fix_forward_reference(annotation)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -68,3 +68,23 @@ def return_annotation_and_docstring_different() -> str:
|
|||||||
|
|
||||||
#? str()
|
#? str()
|
||||||
return_annotation_and_docstring_different()
|
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()
|
||||||
|
|||||||
Reference in New Issue
Block a user