Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix placement of return type when there is a doctest #482

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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