From 876a6a5c223c56f54bad03d29cfea09e98225a3b Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 2 Aug 2019 13:11:41 +0200 Subject: [PATCH] Add ParamDefinition.kind, fixes #1361 --- jedi/api/classes.py | 17 +++++++++++++++-- test/test_api/test_signatures.py | 8 ++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/jedi/api/classes.py b/jedi/api/classes.py index 86453594..caa9a2ec 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -4,10 +4,9 @@ These classes are the much bigger part of the whole API, because they contain the interesting information about completion and goto operations. """ import re +import sys import warnings -from parso.python.tree import search_ancestor - from jedi import settings from jedi import debug from jedi.evaluate.utils import unite @@ -681,6 +680,20 @@ class ParamDefinition(Definition): def to_string(self): return self._name.to_string() + @property + def kind(self): + """ + Returns an enum instance. Returns the same values as the builtin + :py:attr:`inspect.Parameter.kind`. + + No support for Python 2 anymore. + """ + if sys.version_info[0] < 3: + raise NotImplementedError( + 'Python 2 is end-of-life, the new feature is not available for it' + ) + return self._name.get_kind() + def _format_signatures(context): return '\n'.join( diff --git a/test/test_api/test_signatures.py b/test/test_api/test_signatures.py index d051e605..cd86fd87 100644 --- a/test/test_api/test_signatures.py +++ b/test/test_api/test_signatures.py @@ -51,8 +51,11 @@ def test_param_default(Script, code, expected_params): @pytest.mark.parametrize( 'code, index, param_code, kind', [ - ('def f(x=1): ...\nf', 0, 'x=1', ...), - ('def f(*args:int): ...\nf', 0, '*args: int', ...), + ('def f(x=1): ...\nf', 0, 'x=1', 'POSITIONAL_OR_KEYWORD'), + ('def f(*args:int): ...\nf', 0, '*args: int', 'VAR_POSITIONAL'), + ('def f(**kwargs: List[x]): ...\nf', 0, '**kwargs: List[x]', 'VAR_KEYWORD'), + ('def f(*, x:int=5): ...\nf', 0, 'x: int=5', 'KEYWORD_ONLY'), + ('def f(*args, x): ...\nf', 1, 'x', 'KEYWORD_ONLY'), ] ) def test_param_kind_and_name(code, index, param_code, kind, Script, skip_python2): @@ -60,3 +63,4 @@ def test_param_kind_and_name(code, index, param_code, kind, Script, skip_python2 sig, = func.get_signatures() param = sig.params[index] assert param.to_string() == param_code + assert param.kind.name == kind