Skip to content

Commit 3966fb3

Browse files
committed
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.
1 parent 0f42705 commit 3966fb3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ def _inject_types_to_docstring(
633633
return
634634
formatted_annotation = format_annotation(type_hints["return"], app.config)
635635
insert_index = len(lines)
636+
extra_newline = False
636637
for at, line in enumerate(lines):
637638
if line.startswith(":rtype:"):
638639
insert_index = None
@@ -643,16 +644,21 @@ def _inject_types_to_docstring(
643644
break
644645
insert_index = at
645646
elif line.startswith(".."):
646-
# Make sure that rtype comes before any usage or examples section
647+
# Make sure that rtype comes before any usage or examples section, with a blank line between.
647648
insert_index = at
649+
extra_newline = True
648650
break
649651

650652
if insert_index is not None and app.config.typehints_document_rtype:
651653
if insert_index == len(lines): # ensure that :rtype: doesn't get joined with a paragraph of text
652654
lines.append("")
653655
insert_index += 1
654656
if app.config.typehints_use_rtype or insert_index == len(lines):
655-
lines.insert(insert_index, f":rtype: {formatted_annotation}")
657+
line = f":rtype: {formatted_annotation}"
658+
if extra_newline:
659+
lines[insert_index:insert_index] = [line, "\n"]
660+
else:
661+
lines.insert(insert_index, line)
656662
else:
657663
line = lines[insert_index]
658664
lines[insert_index] = f":return: {formatted_annotation} --{line[line.find(' '):]}"

0 commit comments

Comments
 (0)