From 3966fb38b218ec6ffe9204c04aefde667ac97974 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Mon, 16 Jan 2023 13:56:48 +0000 Subject: [PATCH] Fix spacing between `:rtype:` and directives If the `:rtype:` field is being inserted immediately before a directive, we must ensure there is a blank line between the two to avoid a Sphinx warning. --- src/sphinx_autodoc_typehints/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index 997c93e1..6a2a8426 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -633,6 +633,7 @@ def _inject_types_to_docstring( return formatted_annotation = format_annotation(type_hints["return"], app.config) insert_index = len(lines) + extra_newline = False for at, line in enumerate(lines): if line.startswith(":rtype:"): insert_index = None @@ -643,8 +644,9 @@ def _inject_types_to_docstring( break insert_index = at elif line.startswith(".."): - # Make sure that rtype comes before any usage or examples section + # Make sure that rtype comes before any usage or examples section, with a blank line between. insert_index = at + extra_newline = True break if insert_index is not None and app.config.typehints_document_rtype: @@ -652,7 +654,11 @@ def _inject_types_to_docstring( lines.append("") insert_index += 1 if app.config.typehints_use_rtype or insert_index == len(lines): - lines.insert(insert_index, f":rtype: {formatted_annotation}") + line = f":rtype: {formatted_annotation}" + if extra_newline: + lines[insert_index:insert_index] = [line, "\n"] + else: + lines.insert(insert_index, line) else: line = lines[insert_index] lines[insert_index] = f":return: {formatted_annotation} --{line[line.find(' '):]}"