1
0
forked from VimPlug/jedi

initial poc pep-0484 type hints

This commit is contained in:
Claude
2015-12-13 15:20:26 +01:00
parent 8d3be10270
commit fadf4f4419
3 changed files with 54 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ from jedi.evaluate import representation as er
from jedi.evaluate import dynamic from jedi.evaluate import dynamic
from jedi.evaluate import compiled from jedi.evaluate import compiled
from jedi.evaluate import docstrings from jedi.evaluate import docstrings
from jedi.evaluate import pep0484
from jedi.evaluate import iterable from jedi.evaluate import iterable
from jedi.evaluate import imports from jedi.evaluate import imports
from jedi.evaluate import analysis 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__': and func.instance.is_generated and str(func.name) == '__init__':
param = func.var.params[param.position_nr] 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. # Add docstring knowledge.
doc_params = docstrings.follow_param(evaluator, param) doc_params = docstrings.follow_param(evaluator, param)
if doc_params: if doc_params:

34
jedi/evaluate/pep0484.py Normal file
View File

@@ -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 []

View File

@@ -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