Skip to content

Commit 4ab116d

Browse files
authored
Put rtype before examples or usage section (#290)
Sometimes a function's documentation will contain a section like 'Usage' or 'Examples', warnings about edge cases, etc. These sections are introduced with a .. directive::. This makes it so that sphinx-autodoc-typehints inserts the return type ahead of these sections, rather than after them.
1 parent a599552 commit 4ab116d

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ def _inject_types_to_docstring(
625625
insert_index = None
626626
break
627627
insert_index = at
628+
elif line.startswith(".."):
629+
# Make sure that rtype comes before any usage or examples section
630+
insert_index = at
631+
break
628632

629633
if insert_index is not None and app.config.typehints_document_rtype:
630634
if insert_index == len(lines): # ensure that :rtype: doesn't get joined with a paragraph of text

tests/roots/test-dummy/dummy_module.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,13 @@ def mocked_import(x: Mailbox): # noqa: U100
276276
277277
:param x: function
278278
"""
279+
280+
281+
def func_with_examples() -> int:
282+
"""
283+
A docstring.
284+
285+
.. rubric:: Examples
286+
287+
Here are a couple of examples of how to use this function.
288+
"""

tests/roots/test-dummy/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ Dummy Module
3838
.. autodecorator:: dummy_module.Decorator
3939

4040
.. autofunction:: dummy_module.mocked_import
41+
42+
.. autofunction:: dummy_module.func_with_examples

tests/test_sphinx_autodoc_typehints.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,17 @@ class dummy_module.DataClass(x)
748748
749749
Parameters:
750750
**x** ("Mailbox") -- function
751+
752+
dummy_module.func_with_examples()
753+
754+
A docstring.
755+
756+
Return type:
757+
"int"
758+
759+
-[ Examples ]-
760+
761+
Here are a couple of examples of how to use this function.
751762
"""
752763
expected_contents = dedent(expected_contents).format(**format_args).replace("–", "--")
753764
assert text_contents == maybe_fix_py310(expected_contents)

0 commit comments

Comments
 (0)