From e1c0d2c50186523c136dd82287cc79d82267b081 Mon Sep 17 00:00:00 2001 From: Vlad Serebrennikov Date: Sun, 10 May 2020 14:33:36 +0300 Subject: [PATCH] Reduce noise in signatures of compiled params (#1564) * Remove "typing." prefix from compiled signature param * Don't print default "None" for Optional params * Don't remove 'typing.' prefix if symbol doesn't come from typing module * Revert "Don't print default "None" for Optional params" This reverts commit 8db334d9bb77f7810412bdcc4cb090f69eb17012. * Make sure "typing." doesn't appear in the middle * Make sure only "typing." prefix is removed and not it's entries in the middle * Use inspect.formatannotation() to create an annotation string * Update AUTHORS.txt * Add test for compiled param annotation string * Replace Optional in test with other typing facilities in order for test to be forward-compatible with 3.9 * Add an empty string fallback for Python 2 * Move _annotation_to_str back to original position --- AUTHORS.txt | 1 + jedi/inference/compiled/access.py | 6 +++--- test/test_inference/test_mixed.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/AUTHORS.txt b/AUTHORS.txt index f28e1c90..8c3df84f 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -58,6 +58,7 @@ Code Contributors - Lior Goldberg (@goldberglior) - Ryan Clary (@mrclary) - Max Mäusezahl (@mmaeusezahl) +- Vladislav Serebrennikov (@endilll) And a few more "anonymous" contributors. diff --git a/jedi/inference/compiled/access.py b/jedi/inference/compiled/access.py index 99bd843c..a02b548b 100644 --- a/jedi/inference/compiled/access.py +++ b/jedi/inference/compiled/access.py @@ -485,9 +485,9 @@ class DirectObjectAccess(object): return inspect.isclass(self._obj) and self._obj != type def _annotation_to_str(self, annotation): - if isinstance(annotation, type): - return str(annotation.__name__) - return str(annotation) + if py_version < 30: + return '' + return inspect.formatannotation(annotation) def get_signature_params(self): return [ diff --git a/test/test_inference/test_mixed.py b/test/test_inference/test_mixed.py index 7f3c387f..fce1929f 100644 --- a/test/test_inference/test_mixed.py +++ b/test/test_inference/test_mixed.py @@ -102,3 +102,14 @@ def test_signature(): s, = jedi.Interpreter('some_signature', [locals()]).goto() assert s.docstring() == 'some_signature(*, bar=1)' + + +@pytest.mark.skipif(sys.version_info[0:2] < (3, 5), reason="Typing was introduced in Python 3.5") +def test_compiled_signature_annotation_string(): + import typing + def func(x: typing.Type, y: typing.Union[typing.Type, int]): pass + func.__name__ = 'not_func' + + s, = jedi.Interpreter('func()', [locals()]).get_signatures(1, 5) + assert s.params[0].description == 'param x: Type' + assert s.params[1].description == 'param y: Union[Type, int]'