Skip to content

Commit

Permalink
Fix placement of return type when there is a doctest (#482)
Browse files Browse the repository at this point in the history
Resolves #480
  • Loading branch information
hoodmane committed Sep 12, 2024
1 parent 8c8c73d commit 8a90d57
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/sphinx_autodoc_typehints/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from functools import lru_cache
from typing import TYPE_CHECKING, Any

from docutils import nodes
from docutils.parsers.rst.directives.admonitions import BaseAdmonition
from docutils.parsers.rst.states import Text
from docutils.parsers.rst.states import Body, Text
from sphinx.ext.napoleon.docstring import GoogleDocstring

from .attributes_patch import patch_attribute_handling
Expand Down Expand Up @@ -110,6 +111,17 @@ def _patched_text_indent(self: Text, *args: Any) -> Any:
return result


def _patched_body_doctest(
self: Body, _match: None, _context: None, next_state: str | None
) -> tuple[list[Any], str | None, list[Any]]:
line = self.document.current_line + 1
data = "\n".join(self.state_machine.get_text_block())
n = nodes.doctest_block(data, data)
n.line = line
self.parent += n
return [], next_state, []


def _patch_line_numbers() -> None:
"""
Make the rst parser put line numbers on more nodes.
Expand All @@ -118,6 +130,7 @@ def _patch_line_numbers() -> None:
"""
Text.indent = _patched_text_indent
BaseAdmonition.run = _patched_base_admonition_run
Body.doctest = _patched_body_doctest


def install_patches(app: Sphinx) -> None:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,32 @@ def docstring_with_see_also() -> str:
return ""


@expected(
"""
mod.has_doctest1()
Test that we place the return type correctly when the function has
a doctest.
Return type:
"None"
>>> this is a fake doctest
a
>>> more doctest
b
"""
)
def has_doctest1() -> None:
r"""Test that we place the return type correctly when the function has a doctest.
>>> this is a fake doctest
a
>>> more doctest
b
"""


# Config settings for each test run.
# Config Name: Sphinx Options as Dict.
configs = {
Expand Down

0 comments on commit 8a90d57

Please sign in to comment.