Skip to content

Commit

Permalink
Fix issue #813
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlatr committed Sep 22, 2024
1 parent ba2a314 commit ef708aa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
14 changes: 8 additions & 6 deletions pydoctor/astbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from pydoctor.epydoc.markup._pyval_repr import colorize_inline_pyval
from pydoctor.astutils import (is_none_literal, is_typing_annotation, is_using_annotations, is_using_typing_final, node2dottedname, node2fullname,
is__name__equals__main__, unstring_annotation, upgrade_annotation, iterassign, extract_docstring_linenum, infer_type, get_parents,
get_docstring_node, get_assign_docstring_node, extract_doc_comment_before, extract_doc_comment_after, unparse, NodeVisitor, Parentage, Str)
get_docstring_node, get_assign_docstring_node, extract_doc_comment_before, extract_doc_comment_after, validate_inline_docstring_node,
unparse, NodeVisitor, Parentage, Str)

def parseFile(path: Path) -> tuple[ast.Module, Sequence[str]]:
"""
Expand Down Expand Up @@ -857,11 +858,12 @@ def _handleInlineDocstrings(self, assign:Union[ast.Assign, ast.AnnAssign], targe
return

docstring_node = get_assign_docstring_node(assign)
if docstring_node:
# fetch the target of the inline docstring
attr = parent.contents.get(name)
if attr:
attr.setDocstring(docstring_node)
# Validate the docstring, it's not valid if there is a comment in between...
if docstring_node and validate_inline_docstring_node(assign,
docstring_node, self.builder.lines_collection[self.module]
# fetch the target of the inline docstring
) and (attr:=parent.contents.get(name)):
attr.setDocstring(docstring_node)

def visit_AugAssign(self, node:ast.AugAssign) -> None:
try:
Expand Down
14 changes: 13 additions & 1 deletion pydoctor/astutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def get_assign_docstring_node(assign:ast.Assign | ast.AnnAssign) -> Str | None:
This helper function relies on the non-standard C{.parent} attribute on AST nodes
to navigate upward in the tree and determine this node direct siblings.
@note: This does not validate whether there is a comment in between the assigment and the
docstring node since the function operates on AST solely. Use L{validate_inline_docstring_node} for that.
"""
# this call raises an ValueError if we're doing something nasty with the ast... please report
parent_node, fieldname = get_node_block(assign)
Expand Down Expand Up @@ -984,4 +987,13 @@ def extract_doc_comment_before(node: ast.Assign | ast.AnnAssign, lines: Sequence

return None

# This was part of the sphinx.pycode.parser module.
# This was part of the sphinx.pycode.parser module.

def validate_inline_docstring_node(node: ast.Assign | ast.AnnAssign,
docstring: Str,
lines: Sequence[str]) -> bool:
"""
Returns False if the docstring node associated with the given assignment node is not valid.
"""
start, stop = node.lineno, docstring.lineno - 1
return not any(lines[i].lstrip().startswith('#') for i in range(start, stop))
38 changes: 38 additions & 0 deletions pydoctor/test/test_astbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3029,3 +3029,41 @@ def test_inline_docstring_at_wrong_place(systemcls: Type[model.System], capsys:
assert not mod.contents['c'].docstring
assert not mod.contents['d'].docstring
assert not mod.contents['e'].docstring


@systemcls_param
def test_inline_docstring_is_invalid_when_there_is_a_comment_in_between(systemcls: Type[model.System], capsys: CapSys) -> None:
src = '''
a = True
#
'not documentation'
b = True
# b = False
'not documentation'
c = True
# c = False
'not documentation'
d = True
# d = False
"""
not documentation
"""
e = True
# e = False
"""
not documentation
"""
'''

mod = fromText(src, systemcls=systemcls)
assert not capsys.readouterr().out
for o in 'abcde':
assert not mod.contents[o].docstring

0 comments on commit ef708aa

Please sign in to comment.