Skip to content

Commit a427536

Browse files
authored
Fix rendering when docstring starst with a directive (#468)
1 parent fdad0d7 commit a427536

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,15 +849,15 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None:
849849
# at end. (I don't know of any input where this happens.)
850850
next_sibling = child.next_node(descend=False, siblings=True)
851851
line_no = node_line_no(next_sibling) if next_sibling else None
852-
at = line_no - 2 if line_no else len(lines)
852+
at = max(line_no - 2, 0) if line_no else len(lines)
853853
return InsertIndexInfo(insert_index=at, found_param=True)
854854

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

863863
# 5. Otherwise, insert at end

tests/test_integration.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,24 @@ def docstring_with_multiline_note_after_params_epilog_replace(param: int) -> Non
13421342
"""
13431343

13441344

1345+
@expected(
1346+
"""
1347+
mod.docstring_with_see_also()
1348+
1349+
Return type:
1350+
"str"
1351+
1352+
See also: more info at <https://example.com>`_.
1353+
1354+
"""
1355+
)
1356+
def docstring_with_see_also() -> str:
1357+
"""
1358+
.. seealso:: more info at <https://example.com>`_.
1359+
"""
1360+
return ""
1361+
1362+
13451363
# Config settings for each test run.
13461364
# Config Name: Sphinx Options as Dict.
13471365
configs = {

0 commit comments

Comments
 (0)