From ee3de71540a625e6c495dc2c6b95aa2b657a4365 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 19 Jan 2023 21:33:35 -0800 Subject: [PATCH 1/4] Resolves issue 305 Apparently we need to subtract 3 here? --- src/sphinx_autodoc_typehints/__init__.py | 2 +- tests/test_integration.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index ff59a952..9d8cc780 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -716,7 +716,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 = line_no - 3 if line_no else len(lines) return InsertIndexInfo(insert_index=at, found_param=True) # 4. Insert before examples diff --git a/tests/test_integration.py b/tests/test_integration.py index 08854586..b9da2450 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -874,6 +874,25 @@ def func_with_definition_list() -> int: # See https://github.com/tox-dev/sphinx-autodoc-typehints/issues/302 +@expected("") +def decorator_2(f: Any) -> Any: + """Run the decorated function with `asyncio.run`. + + Parameters + ---------- + f + The function to wrap. + + Examples + -------- + + .. code-block:: python + + A + """ + f + + AUTO_FUNCTION = ".. autofunction:: mod.{}" AUTO_CLASS = """\ .. autoclass:: mod.{} From 5e3aaa641d5c84a23b24ce95515d8ed1a3f2c856 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 19 Jan 2023 22:04:27 -0800 Subject: [PATCH 2/4] Fix test --- tests/test_integration.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_integration.py b/tests/test_integration.py index b9da2450..2907dbf8 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -874,7 +874,23 @@ def func_with_definition_list() -> int: # See https://github.com/tox-dev/sphinx-autodoc-typehints/issues/302 -@expected("") +@expected( + """\ +mod.decorator_2(f) + + Run the decorated function with *asyncio.run*. + + Parameters: + **f** ("Any") -- The function to wrap. + + Return type: + "Any" + + -[ Examples ]- + + A +""" +) def decorator_2(f: Any) -> Any: """Run the decorated function with `asyncio.run`. @@ -933,5 +949,5 @@ def test_integration(app: SphinxTestApp, status: StringIO, warning: StringIO, mo try: assert result.strip() == dedent(expected).strip() except Exception: - print("Result was:\n", result, "\n\n") + print(f"Result was:\n{result}\n\n") raise From 55bee04aaecf9bcfbd0ed7d357c7931a71a41b30 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 19 Jan 2023 22:49:48 -0800 Subject: [PATCH 3/4] Fix test again --- src/sphinx_autodoc_typehints/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index 9d8cc780..c87d0d27 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -716,7 +716,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 - 3 if line_no else len(lines) + at = line_no - 2 if line_no else len(lines) return InsertIndexInfo(insert_index=at, found_param=True) # 4. Insert before examples @@ -758,6 +758,9 @@ def _inject_rtype( formatted_annotation = format_annotation(type_hints["return"], app.config) + if r.found_param and insert_index < len(lines) and lines[insert_index].strip() != "": + insert_index -= 1 + if insert_index == len(lines) and not r.found_param: # ensure that :rtype: doesn't get joined with a paragraph of text lines.append("") From 6c4933b9fe6a84823c594a2da216c5cdc640a8d1 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 20 Jan 2023 08:11:24 -0800 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73824f79..c2f74891 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] + +- Fix a `Field list ends without a blank line` warning (issue 305). + ## 1.21.5 - More robust determination of rtype location / fix issue 302