Skip to content

Commit

Permalink
Fix spacing between :rtype: and directives
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jakelishman committed Jan 16, 2023
1 parent 0f42705 commit 3966fb3
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -643,16 +644,21 @@ 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:
if insert_index == len(lines): # ensure that :rtype: doesn't get joined with a paragraph of text
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(' '):]}"
Expand Down

0 comments on commit 3966fb3

Please sign in to comment.