Skip to content

Commit 752764e

Browse files
committed
Add typehints_rtype_after_returns option
* Allow the user to specify the order of the rtype/returns statements. * Fix an index error exception caused when always_document_param_types is True and typehints_defaults is 'braces-after'.
1 parent 4b19ed2 commit 752764e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ The following configuration options are accepted:
7272
directive, if present, otherwise fall back to using `:rtype:`. Use in conjunction with
7373
[napoleon_use_rtype](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#confval-napoleon_use_rtype)
7474
to avoid generation of duplicate or redundant return type information.
75+
- `typehints_rtype_after_returns` (default `False`): Controls behavior when `typehints_document_rtype` and
76+
`typehints_use_rtype` are set to True. If `True` insert the `:rtype:` after the `:returns:`. If False
77+
insert the `:rtype:` before the `:returns:`.
7578
- `typehints_defaults` (default: `None`): If `None`, defaults are not added. Otherwise, adds a default annotation:
7679

7780
- `'comma'` adds it after the type, changing Sphinx’ default look to “**param** (_int_, default: `1`) -- text”.

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,11 @@ def _append_default(
778778
if lines[next_index]:
779779
append_index = next_index
780780
next_index += 1
781-
lines[append_index] += formatted_default
782-
781+
if append_index >= nlines:
782+
# When append_index is greater than nlines, simply append the line to the end of lines
783+
lines.append(formatted_default)
784+
else:
785+
lines[append_index] += formatted_default
783786
else: # add to last param doc line
784787
type_annotation += formatted_default
785788

@@ -895,6 +898,9 @@ def _inject_rtype( # noqa: PLR0913, PLR0917
895898

896899
formatted_annotation = add_type_css_class(format_annotation(type_hints["return"], app.config))
897900

901+
if app.config.typehints_rtype_after_returns and r.found_return:
902+
insert_index += 1
903+
898904
if r.found_param and insert_index < len(lines) and lines[insert_index].strip():
899905
insert_index -= 1
900906

@@ -966,6 +972,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
966972
app.add_config_value("typehints_fully_qualified", False, "env") # noqa: FBT003
967973
app.add_config_value("typehints_document_rtype", True, "env") # noqa: FBT003
968974
app.add_config_value("typehints_use_rtype", True, "env") # noqa: FBT003
975+
app.add_config_value("typehints_rtype_after_returns", False, "env") # noqa: FBT003
969976
app.add_config_value("typehints_defaults", None, "env")
970977
app.add_config_value("simplify_optional_unions", True, "env") # noqa: FBT003
971978
app.add_config_value("always_use_bars_union", False, "env") # noqa: FBT003

0 commit comments

Comments
 (0)