From 07de5cfe1354396f7ad8da3972f615159a80ef93 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 17 Jul 2024 11:42:41 +0200 Subject: [PATCH] Fix rendering when docstring starst with a directive 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 --- src/sphinx_autodoc_typehints/__init__.py | 4 ++-- tests/test_integration.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index d5b062a..035c6db 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -849,7 +849,7 @@ 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 @@ -857,7 +857,7 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None: 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 diff --git a/tests/test_integration.py b/tests/test_integration.py index 8d732d9..a9aec4c 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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 `_. + + """ +) +def docstring_with_see_also() -> str: + """ + .. seealso:: more info at `_. + """ + return "" + + # Config settings for each test run. # Config Name: Sphinx Options as Dict. configs = {