Repository: tox-dev/sphinx-autodoc-typehints (python)

Issue:
WARNING: Field list ends without a blank line; unexpected unindent
The `WARNING: Field list ends without a blank line; unexpected unindent` has returned in 1.21.5. I suspect this is another variation on the problem reported and fixed in #293.

GitHub Actions log showing this failure: https://github.com/lsst-sqre/safir/actions/runs/3963428111/jobs/6791355176

Example docstring that produces the first error message there:

```python
    """Run the decorated function with `asyncio.run`.

    Intended to be used as a decorator around an async function that needs to
    be run in a sync context.  The decorated function will be run with
    `asyncio.run` when invoked.  The caller must not already be inside an
    asyncio task.

    Parameters
    ----------
    f
        The function to wrap.

    Examples
    --------
    An application that uses Safir and `Click`_ may use the following Click
    command function to initialize a database.

    .. code-block:: python

       import structlog
       from safir.asyncio import run_with_asyncio
       from safir.database import initialize_database

       from .config import config
       from .schema import Base


       @main.command()
       @run_with_asyncio
       async def init() -> None:
           logger = structlog.get_logger(config.safir.logger_name)
           engine = await initialize_database(
               config.database_url,
               config.database_password,
               logger,
               schema=Base.metadata,
           )
           await engine.dispose()
    """
```

Removing the `Parameters` section makes the warning go away. The same docstring (and the others in this package) worked correctly with 1.21.1.

Pull request description:
Resolve issue 305
Apparently we need to subtract 3 here?

Resolves #305.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 73824f7..c2f7489 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
diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py
index ff59a95..c87d0d2 100644
--- a/src/sphinx_autodoc_typehints/__init__.py
+++ b/src/sphinx_autodoc_typehints/__init__.py
@@ -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("")


Test patch:
diff --git a/tests/test_integration.py b/tests/test_integration.py
index 0885458..2907dbf 100644
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -874,6 +874,41 @@ def func_with_definition_list() -> int:
     # See https://github.com/tox-dev/sphinx-autodoc-typehints/issues/302
 
 
+@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`.
+
+    Parameters
+    ----------
+    f
+        The function to wrap.
+
+    Examples
+    --------
+
+    .. code-block:: python
+
+        A
+    """
+    f
+
+
 AUTO_FUNCTION = ".. autofunction:: mod.{}"
 AUTO_CLASS = """\
 .. autoclass:: mod.{}
@@ -914,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