diff --git a/jedi/evaluate/finder.py b/jedi/evaluate/finder.py index 89de08e4..ad2a6c33 100644 --- a/jedi/evaluate/finder.py +++ b/jedi/evaluate/finder.py @@ -23,6 +23,7 @@ from jedi.evaluate import representation as er from jedi.evaluate import dynamic from jedi.evaluate import compiled from jedi.evaluate import docstrings +from jedi.evaluate import pep0484 from jedi.evaluate import iterable from jedi.evaluate import imports from jedi.evaluate import analysis @@ -387,6 +388,11 @@ def _eval_param(evaluator, param, scope): and func.instance.is_generated and str(func.name) == '__init__': param = func.var.params[param.position_nr] + # Add pep0484 type hints + pep0484_hints = pep0484.follow_param(evaluator, param) + if pep0484_hints: + return pep0484_hints + # Add docstring knowledge. doc_params = docstrings.follow_param(evaluator, param) if doc_params: diff --git a/jedi/evaluate/pep0484.py b/jedi/evaluate/pep0484.py new file mode 100644 index 00000000..97433502 --- /dev/null +++ b/jedi/evaluate/pep0484.py @@ -0,0 +1,34 @@ +""" +PEP 0484 ( https://www.python.org/dev/peps/pep-0484/ ) describes type hints +through function annotations. There is a strong suggestion in this document +that only the type of type hinting defined in PEP0484 should be allowed +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 +x 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) +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 +""" + +from itertools import chain + +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]) + return list(chain.from_iterable( + evaluator.execute(d) for d in definitions)) + else: + return [] diff --git a/test/completion/pep0484.py b/test/completion/pep0484.py new file mode 100644 index 00000000..dac4f17a --- /dev/null +++ b/test/completion/pep0484.py @@ -0,0 +1,14 @@ +""" Pep-0484 type hinting """ + +# ----------------- +# sphinx style +# ----------------- +def typehints(a, b: str, c: int, d:int = 4): + #? + a + #? str() + b + #? int() + c + #? int() + d