Skip to content

Commit

Permalink
Attribute docstrings work now, fixes #138
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalter committed Dec 22, 2019
1 parent 4161bfc commit fc785ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 4 additions & 3 deletions jedi/inference/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from parso.tree import search_ancestor

from jedi._compatibility import Parameter
from jedi.parser_utils import clean_scope_docstring
from jedi.parser_utils import clean_scope_docstring, find_statement_documentation
from jedi.inference.utils import unite
from jedi.inference.base_value import ValueSet, NO_VALUES
from jedi.inference import docstrings
Expand Down Expand Up @@ -324,10 +324,12 @@ 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 == '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:
print('la', _merge_name_docs(names))
return _merge_name_docs(names)
return super(TreeNameDefinition, self).py__doc__(include_signatures)

Expand Down Expand Up @@ -578,7 +580,6 @@ def api_type(self):
return 'module'

def py__doc__(self, include_signatures=False):
print('la', (self.goto()))
return _merge_name_docs(self.goto())


Expand Down
15 changes: 15 additions & 0 deletions jedi/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ def clean_scope_docstring(scope_node):
return ''


def find_statement_documentation(tree_node):
if tree_node.type == 'expr_stmt':
tree_node = tree_node.parent # simple_stmt
maybe_string = tree_node.get_next_sibling()
if maybe_string is not None:
if maybe_string.type == 'simple_stmt':
maybe_string = maybe_string.children[0]
if maybe_string.type == 'string':
cleaned = cleandoc(safe_literal_eval(maybe_string.value))
# Since we want the docstr output to be always unicode, just
# force it.
return force_unicode(cleaned)
return ''


def safe_literal_eval(value):
first_two = value[:2].lower()
if first_two[0] == 'f' or first_two in ('fr', 'rf'):
Expand Down
23 changes: 23 additions & 0 deletions test/test_api/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,26 @@ def test_lambda(Script):
)
def test_help_no_returns(Script, code, kwargs):
assert not Script(code).help(**kwargs)


def test_attribute_docstrings(goto_or_help):
code = dedent('''\
class X:
"ha"
x = 3
""" Yeah """
y = 5
"f g "
z = lambda: 1
''')

d, = goto_or_help(code + 'X.x')
assert d.docstring() == 'Yeah '
d, = goto_or_help(code + 'X().x')
assert d.docstring() == 'Yeah '

d, = goto_or_help(code + 'X.y')
assert d.docstring() == 'f g '

d, = goto_or_help(code + 'X.z')
assert d.docstring() == ''

0 comments on commit fc785ce

Please sign in to comment.