forked from VimPlug/jedi
string-annotations should only be interpreted by the pep-0484 code, not the parser
This commit is contained in:
@@ -17,13 +17,23 @@ x support `@no_type_check` and `@no_type_check_decorator`
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
from jedi.parser import Parser, load_grammar
|
||||||
from jedi.evaluate.cache import memoize_default
|
from jedi.evaluate.cache import memoize_default
|
||||||
|
from jedi.evaluate.compiled import CompiledObject
|
||||||
|
|
||||||
|
|
||||||
def _evaluate_for_annotation(evaluator, annotation):
|
def _evaluate_for_annotation(evaluator, annotation):
|
||||||
if annotation is not None:
|
if annotation is not None:
|
||||||
definitions = evaluator.eval_element(annotation)
|
definitions = set()
|
||||||
|
for definition in evaluator.eval_element(annotation):
|
||||||
|
if (isinstance(definition, CompiledObject) and
|
||||||
|
isinstance(definition.obj, str)):
|
||||||
|
p = Parser(load_grammar(), definition.obj)
|
||||||
|
element = p.module.children[0].children[0]
|
||||||
|
element.parent = annotation.parent
|
||||||
|
definitions |= evaluator.eval_element(element)
|
||||||
|
else:
|
||||||
|
definitions.add(definition)
|
||||||
return list(chain.from_iterable(
|
return list(chain.from_iterable(
|
||||||
evaluator.execute(d) for d in definitions))
|
evaluator.execute(d) for d in definitions))
|
||||||
else:
|
else:
|
||||||
|
|||||||
+3
-16
@@ -41,7 +41,6 @@ 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):
|
||||||
@@ -53,19 +52,6 @@ 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):
|
||||||
@@ -881,8 +867,9 @@ class Function(ClassOrFunc):
|
|||||||
def annotation(self):
|
def annotation(self):
|
||||||
try:
|
try:
|
||||||
if self.children[3] == "->":
|
if self.children[3] == "->":
|
||||||
return _fix_forward_reference(self.children[4])
|
return self.children[4]
|
||||||
assert self.children[3] == ":"
|
assert self.children[3] == ":"
|
||||||
|
return None
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -1428,7 +1415,7 @@ class Param(BaseNode):
|
|||||||
assert tfpdef.children[1] == ":"
|
assert tfpdef.children[1] == ":"
|
||||||
assert len(tfpdef.children) == 3
|
assert len(tfpdef.children) == 3
|
||||||
annotation = tfpdef.children[2]
|
annotation = tfpdef.children[2]
|
||||||
return _fix_forward_reference(annotation)
|
return annotation
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user