mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Make sure that include_signature always works, fixes #1466
This commit is contained in:
@@ -12,14 +12,14 @@ from jedi.inference.helpers import deep_ast_copy, infer_call_of_leaf
|
|||||||
from jedi.plugins import plugin_manager
|
from jedi.plugins import plugin_manager
|
||||||
|
|
||||||
|
|
||||||
def _merge_name_docs(names):
|
def _merge_name_docs(names, include_signatures):
|
||||||
doc = ''
|
doc = ''
|
||||||
for name in names:
|
for name in names:
|
||||||
if doc:
|
if doc:
|
||||||
# In case we have multiple values, just return all of them
|
# In case we have multiple values, just return all of them
|
||||||
# separated by a few dashes.
|
# separated by a few dashes.
|
||||||
doc += '\n' + '-' * 30 + '\n'
|
doc += '\n' + '-' * 30 + '\n'
|
||||||
doc += name.py__doc__()
|
doc += name.py__doc__(include_signatures)
|
||||||
return doc
|
return doc
|
||||||
|
|
||||||
|
|
||||||
@@ -322,16 +322,13 @@ class TreeNameDefinition(AbstractTreeName):
|
|||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
def py__doc__(self, include_signatures=False):
|
def py__doc__(self, include_signatures=False):
|
||||||
if self.api_type in ('function', 'class'):
|
if self.api_type in ('function', 'class', 'module'):
|
||||||
return clean_scope_docstring(self.tree_name.get_definition())
|
# 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():
|
if self.api_type == 'statement' and self.tree_name.is_definition():
|
||||||
return find_statement_documentation(self.tree_name.get_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)
|
return super(TreeNameDefinition, self).py__doc__(include_signatures)
|
||||||
|
|
||||||
|
|
||||||
@@ -583,7 +580,7 @@ class ImportName(AbstractNameDefinition):
|
|||||||
return 'module'
|
return 'module'
|
||||||
|
|
||||||
def py__doc__(self, include_signatures=False):
|
def py__doc__(self, include_signatures=False):
|
||||||
return _merge_name_docs(self.goto())
|
return _merge_name_docs(self.goto(), include_signatures)
|
||||||
|
|
||||||
|
|
||||||
class SubModuleName(ImportName):
|
class SubModuleName(ImportName):
|
||||||
@@ -612,8 +609,10 @@ class StubNameMixin(object):
|
|||||||
if self in names:
|
if self in names:
|
||||||
doc = super(StubNameMixin, self).py__doc__(include_signatures)
|
doc = super(StubNameMixin, self).py__doc__(include_signatures)
|
||||||
else:
|
else:
|
||||||
doc = _merge_name_docs(names)
|
# We have signatures ourselves in stubs, so don't use signatures
|
||||||
if include_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
|
parent = self.tree_name.parent
|
||||||
if parent.type in ('funcdef', 'classdef') and parent.name is self.tree_name:
|
if parent.type in ('funcdef', 'classdef') and parent.name is self.tree_name:
|
||||||
doc = _merge_docs_and_signature(self.infer(), doc)
|
doc = _merge_docs_and_signature(self.infer(), doc)
|
||||||
|
|||||||
@@ -1,2 +1,9 @@
|
|||||||
in_with_stub_both = 5
|
in_with_stub_both = 5
|
||||||
in_with_stub_python = 8
|
in_with_stub_python = 8
|
||||||
|
|
||||||
|
|
||||||
|
def stub_function(x: float, y):
|
||||||
|
"""
|
||||||
|
Python docstring
|
||||||
|
"""
|
||||||
|
return 1
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
in_with_stub_both: str
|
in_with_stub_both: str
|
||||||
in_with_stub_stub: float
|
in_with_stub_stub: float
|
||||||
|
|
||||||
|
|
||||||
|
def stub_function(x: int, y: float) -> str:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -2,11 +2,14 @@
|
|||||||
Testing of docstring related issues and especially ``jedi.docstrings``.
|
Testing of docstring related issues and especially ``jedi.docstrings``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from textwrap import dedent
|
import os
|
||||||
import jedi
|
|
||||||
import pytest
|
|
||||||
from ..helpers import unittest
|
|
||||||
import sys
|
import sys
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import jedi
|
||||||
|
from ..helpers import unittest, test_dir
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import numpydoc # NOQA
|
import numpydoc # NOQA
|
||||||
@@ -171,6 +174,22 @@ def test_docstring_params_formatting(Script):
|
|||||||
assert defs[0].docstring() == 'func(param1, param2, param3)'
|
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 ---
|
# ---- Numpy Style Tests ---
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
|
|||||||
Reference in New Issue
Block a user