diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index 6e68ee0..3eb0764 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -750,20 +750,31 @@ def _inject_signature( # noqa: C901 if app.config.typehints_defaults: formatted_default = format_default(app, default, annotation is not None) if formatted_default: - if app.config.typehints_defaults.endswith("after"): + type_annotation = _append_default(app, lines, insert_index, type_annotation, formatted_default) - # advance index to the end of the :param: paragraph - nlines = len(lines) - i = insert_index - while i < nlines and (lines[i + 1] == "" or lines[i + 1].startswith(" ")): - i = i + 1 - - lines[i] += formatted_default + lines.insert(insert_index, type_annotation) - else: # add to last param doc line - type_annotation += formatted_default - lines.insert(insert_index, type_annotation) +def _append_default( + app: Sphinx, lines: list[str], insert_index: int, type_annotation: str, formatted_default: str +) -> str: + if app.config.typehints_defaults.endswith("after"): + # advance the index to the end of the :param: paragraphs + # (terminated by a line with no indentation) + # append default to the last nonempty line + nlines = len(lines) + i = insert_index + 1 + j = insert_index # last nonempty line + while i < nlines and (not lines[i] or lines[i].startswith(" ")): + if lines[i]: + j = i + i += 1 + lines[j] += formatted_default + + else: # add to last param doc line + type_annotation += formatted_default + + return type_annotation @dataclass diff --git a/tests/test_integration_issue_384.py b/tests/test_integration_issue_384.py index 83acef9..66815e6 100644 --- a/tests/test_integration_issue_384.py +++ b/tests/test_integration_issue_384.py @@ -4,7 +4,7 @@ import sys from pathlib import Path from textwrap import dedent, indent -from typing import TYPE_CHECKING, Any, Callable, Literal, NewType, TypeVar # no type comments +from typing import TYPE_CHECKING, Any, Callable, NewType, TypeVar # no type comments import pytest @@ -36,61 +36,42 @@ def dec(val: T) -> T: @expected( """\ -mod.function(x=5) +mod.function(x=5, y=10, z=15) Function docstring. Parameters: - **x** ("int") -- optional specifier for how to handle complex - data types. See "ivy.func_wrapper.handle_complex_input" for more - detail. - - Returns: - something + * **x** ("int") -- optional specifier line 2 (default: "5") - Return type: - bytes -""", -) -def function1(x: int, y: int, z: int) -> str: # noqa: ARG001 - """ - Function docstring. + * **y** ("int") -- - :param x: optional specifier for how to handle complex data types. See - ``ivy.func_wrapper.handle_complex_input`` for more detail. - :param y: another optional specifier for how to handle complex data types. See - ``ivy.func_wrapper.handle_complex_input`` for more detail. + another optional line 4 - :param z: yet another optional specifier for how to handle complex data types. See - ``ivy.func_wrapper.handle_complex_input`` for more detail. - :return: something - :rtype: bytes - """ + second paragraph for y (default: "10") -@expected( - """\ -mod.function(x=5) - - Function docstring. + * **z** ("int") -- yet another optional s line 6 (default: "15") - Parameters: - **x** ("int") -- optional specifier for how to handle complex - data types. See "ivy.func_wrapper.handle_complex_input" for more - detail. - Returns: something Return type: bytes + """, ) -def function(x: int = 5, y: int = 10, z: int = 5) -> str: # noqa: ARG001 +def function(x: int = 5, y: int = 10, z: int = 15) -> str: # noqa: ARG001 """ Function docstring. - :param x: optional specifier + :param x: optional specifier line 2 + :param y: another optional + line 4 + + second paragraph for y + + :param z: yet another optional s + line 6 :return: something :rtype: bytes