Skip to content

Commit

Permalink
Fix rendering when docstring starst with a directive
Browse files Browse the repository at this point in the history
We want to position our insertion before the directive but we incorrectly get a
negative insertion line. This inserts at the end rather than at the beginning,
and we fail to leave the correct empty line after the directive.

Resolves #467
  • Loading branch information
hoodmane committed Jul 17, 2024
1 parent df66980 commit 07de5cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,15 +849,15 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None:
# at end. (I don't know of any input where this happens.)
next_sibling = child.next_node(descend=False, siblings=True)
line_no = node_line_no(next_sibling) if next_sibling else None
at = line_no - 2 if line_no else len(lines)
at = max(line_no - 2, 0) if line_no else len(lines)
return InsertIndexInfo(insert_index=at, found_param=True)

# 4. Insert before examples
for child in doc.children:
if tag_name(child) in {"literal_block", "paragraph", "field_list"}:
continue
line_no = node_line_no(child)
at = line_no - 2 if line_no else len(lines)
at = max(line_no - 2, 0) if line_no else len(lines)
return InsertIndexInfo(insert_index=at, found_directive=True)

# 5. Otherwise, insert at end
Expand Down
18 changes: 18 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,24 @@ def docstring_with_multiline_note_after_params_epilog_replace(param: int) -> Non
"""


@expected(
"""
mod.docstring_with_see_also()
Return type:
"str"
See also: more info at <https://example.com>`_.
"""
)
def docstring_with_see_also() -> str:
"""
.. seealso:: more info at <https://example.com>`_.
"""
return ""


# Config settings for each test run.
# Config Name: Sphinx Options as Dict.
configs = {
Expand Down

0 comments on commit 07de5cf

Please sign in to comment.