diff --git a/jedi/evaluate/pep0484.py b/jedi/evaluate/pep0484.py index 97433502..1256ef2d 100644 --- a/jedi/evaluate/pep0484.py +++ b/jedi/evaluate/pep0484.py @@ -13,6 +13,7 @@ x Local variable type hints x Assigned types: `Url = str\ndef get(url:Url) -> str:` x Type hints in `with` statements x Stub files support +x support `@no_type_check` and `@no_type_check_decorator` """ from itertools import chain @@ -22,12 +23,9 @@ from jedi.evaluate.cache import memoize_default @memoize_default(None, evaluator_is_first_arg=True) def follow_param(evaluator, param): - # annotation is in param.children[0] if present - # either this firstchild is a Name (if no annotation is present) or a Node - if hasattr(param.children[0], "children"): - assert len(param.children[0].children) == 3 and \ - param.children[0].children[1] == ":" - definitions = evaluator.eval_element(param.children[0].children[2]) + annotation = param.annotation() + if annotation: + definitions = evaluator.eval_element(annotation) return list(chain.from_iterable( evaluator.execute(d) for d in definitions)) else: diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 5a871671..7f0e1960 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -1403,8 +1403,13 @@ class Param(BaseNode): return None def annotation(self): - # Generate from tfpdef. - raise NotImplementedError + tfpdef = self._tfpdef() + if is_node(tfpdef, 'tfpdef'): + assert tfpdef.children[1] == ":" + assert len(tfpdef.children) == 3 + return tfpdef.children[2] + else: + return None def _tfpdef(self): """ diff --git a/test/completion/pep0484.py b/test/completion/pep0484.py index dac4f17a..64f656fa 100644 --- a/test/completion/pep0484.py +++ b/test/completion/pep0484.py @@ -1,10 +1,12 @@ """ Pep-0484 type hinting """ # ----------------- -# sphinx style +# simple classes # ----------------- -def typehints(a, b: str, c: int, d:int = 4): - #? + + +def typehints(a, b: str, c: int, d: int=4): + #? a #? str() b diff --git a/test/test_evaluate/test_annotations.py b/test/test_evaluate/test_annotations.py index 1fefde3c..1a26a1bc 100644 --- a/test/test_evaluate/test_annotations.py +++ b/test/test_evaluate/test_annotations.py @@ -8,8 +8,8 @@ import pytest def test_simple_annotations(): """ Annotations only exist in Python 3. - At the moment we ignore them. So they should be parsed and not interfere - with anything. + If annotations adhere to PEP-0484, we use them (they override inference), + else they are parsed but ignored """ source = dedent("""\ @@ -27,3 +27,11 @@ def test_simple_annotations(): annot_ret('')""") assert [d.name for d in jedi.Script(source, ).goto_definitions()] == ['str'] + + source = dedent("""\ + def annot(a:int): + return a + + annot('')""") + + assert [d.name for d in jedi.Script(source, ).goto_definitions()] == ['int']