string-annotations should only be interpreted by the pep-0484 code, not the parser

This commit is contained in:
Claude
2015-12-15 00:31:47 +01:00
parent 626fa60d03
commit 3cef8b6d55
2 changed files with 15 additions and 18 deletions

View File

@@ -17,13 +17,23 @@ x support `@no_type_check` and `@no_type_check_decorator`
"""
from itertools import chain
from jedi.parser import Parser, load_grammar
from jedi.evaluate.cache import memoize_default
from jedi.evaluate.compiled import CompiledObject
def _evaluate_for_annotation(evaluator, annotation):
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(
evaluator.execute(d) for d in definitions))
else:

View File

@@ -41,7 +41,6 @@ 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):
@@ -53,19 +52,6 @@ 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):
@@ -881,8 +867,9 @@ class Function(ClassOrFunc):
def annotation(self):
try:
if self.children[3] == "->":
return _fix_forward_reference(self.children[4])
return self.children[4]
assert self.children[3] == ":"
return None
except IndexError:
return None
@@ -1428,7 +1415,7 @@ class Param(BaseNode):
assert tfpdef.children[1] == ":"
assert len(tfpdef.children) == 3
annotation = tfpdef.children[2]
return _fix_forward_reference(annotation)
return annotation
else:
return None