From 8c8c73d9794eb84c1639ee6c6c4758bf5880460c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:13:48 -0700 Subject: [PATCH 1/2] [pre-commit.ci] pre-commit autoupdate (#478) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/tox-dev/pyproject-fmt: 2.2.1 → 2.2.3](https://github.com/tox-dev/pyproject-fmt/compare/2.2.1...2.2.3) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4b981c1..2556d24 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: - id: tox-ini-fmt args: ["-p", "fix"] - repo: https://github.com/tox-dev/pyproject-fmt - rev: "2.2.1" + rev: "2.2.3" hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit From 8a90d5725be01ecfdc77d55b1c44038f7dce6bce Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 12 Sep 2024 15:59:25 +0200 Subject: [PATCH 2/2] Fix placement of return type when there is a doctest (#482) Resolves #480 --- src/sphinx_autodoc_typehints/patches.py | 15 +++++++++++++- tests/test_integration.py | 26 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/patches.py b/src/sphinx_autodoc_typehints/patches.py index e29a6dc..2307f53 100644 --- a/src/sphinx_autodoc_typehints/patches.py +++ b/src/sphinx_autodoc_typehints/patches.py @@ -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 @@ -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. @@ -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: diff --git a/tests/test_integration.py b/tests/test_integration.py index cbd218e..3339916 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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 = {