From 8770e12d1649b8682aee32b9ca308a1a4e79019e Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 1 Jan 2020 15:13:53 +0100 Subject: [PATCH] Make sure that include_signature always works, fixes #1466 --- jedi/inference/names.py | 21 +++++++++--------- test/completion/stub_folder/with_stub.py | 7 ++++++ test/completion/stub_folder/with_stub.pyi | 4 ++++ test/test_inference/test_docstring.py | 27 +++++++++++++++++++---- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/jedi/inference/names.py b/jedi/inference/names.py index effc2410..e1ee9c94 100644 --- a/jedi/inference/names.py +++ b/jedi/inference/names.py @@ -12,14 +12,14 @@ from jedi.inference.helpers import deep_ast_copy, infer_call_of_leaf from jedi.plugins import plugin_manager -def _merge_name_docs(names): +def _merge_name_docs(names, include_signatures): doc = '' for name in names: if doc: # In case we have multiple values, just return all of them # separated by a few dashes. doc += '\n' + '-' * 30 + '\n' - doc += name.py__doc__() + doc += name.py__doc__(include_signatures) return doc @@ -322,16 +322,13 @@ class TreeNameDefinition(AbstractTreeName): return indexes def py__doc__(self, include_signatures=False): - if self.api_type in ('function', 'class'): - return clean_scope_docstring(self.tree_name.get_definition()) + if self.api_type in ('function', 'class', 'module'): + # Make sure the names are not TreeNameDefinitions anymore. + return _merge_name_docs([v.name for v in self.infer()], include_signatures) if self.api_type == 'statement' and self.tree_name.is_definition(): return find_statement_documentation(self.tree_name.get_definition()) - if self.api_type == 'module': - names = self.goto() - if self not in names: - return _merge_name_docs(names) return super(TreeNameDefinition, self).py__doc__(include_signatures) @@ -583,7 +580,7 @@ class ImportName(AbstractNameDefinition): return 'module' def py__doc__(self, include_signatures=False): - return _merge_name_docs(self.goto()) + return _merge_name_docs(self.goto(), include_signatures) class SubModuleName(ImportName): @@ -612,8 +609,10 @@ class StubNameMixin(object): if self in names: doc = super(StubNameMixin, self).py__doc__(include_signatures) else: - doc = _merge_name_docs(names) - if include_signatures: + # We have signatures ourselves in stubs, so don't use signatures + # from the implementation. + doc = _merge_name_docs(names, include_signatures=False) + if include_signatures and self.tree_name is not None: parent = self.tree_name.parent if parent.type in ('funcdef', 'classdef') and parent.name is self.tree_name: doc = _merge_docs_and_signature(self.infer(), doc) diff --git a/test/completion/stub_folder/with_stub.py b/test/completion/stub_folder/with_stub.py index 7f064dca..a471fa91 100644 --- a/test/completion/stub_folder/with_stub.py +++ b/test/completion/stub_folder/with_stub.py @@ -1,2 +1,9 @@ in_with_stub_both = 5 in_with_stub_python = 8 + + +def stub_function(x: float, y): + """ + Python docstring + """ + return 1 diff --git a/test/completion/stub_folder/with_stub.pyi b/test/completion/stub_folder/with_stub.pyi index 7a3f3ecf..973528cc 100644 --- a/test/completion/stub_folder/with_stub.pyi +++ b/test/completion/stub_folder/with_stub.pyi @@ -1,2 +1,6 @@ in_with_stub_both: str in_with_stub_stub: float + + +def stub_function(x: int, y: float) -> str: + pass diff --git a/test/test_inference/test_docstring.py b/test/test_inference/test_docstring.py index cf2e1724..74a3218c 100644 --- a/test/test_inference/test_docstring.py +++ b/test/test_inference/test_docstring.py @@ -2,11 +2,14 @@ Testing of docstring related issues and especially ``jedi.docstrings``. """ -from textwrap import dedent -import jedi -import pytest -from ..helpers import unittest +import os import sys +from textwrap import dedent + +import pytest + +import jedi +from ..helpers import unittest, test_dir try: import numpydoc # NOQA @@ -171,6 +174,22 @@ def test_docstring_params_formatting(Script): assert defs[0].docstring() == 'func(param1, param2, param3)' +def test_import_function_docstring(Script, skip_pre_python35): + code = "from stub_folder import with_stub; with_stub.stub_function" + path = os.path.join(test_dir, 'completion', 'import_function_docstring.py') + c, = Script(code, path=path).complete() + + stub_signature = 'stub_function(x: int, y: float) -> str' + python_signature = 'stub_function(x: float, y)' + doc = '\n\nPython docstring' + assert c.docstring() == stub_signature + doc + assert c.type == 'function' + func, = c.goto(prefer_stubs=True) + assert func.docstring() == stub_signature + doc + func, = c.goto() + assert func.docstring() == python_signature + doc + + # ---- Numpy Style Tests --- @pytest.mark.skipif(numpydoc_unavailable,