Skip to content

Commit 53f7f00

Browse files
Disable GoogleDocstring._lookup_annotation (#309)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Resolves #308
1 parent 15c6456 commit 53f7f00

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sphinx.environment import BuildEnvironment
2020
from sphinx.ext.autodoc import Options
2121
from sphinx.ext.autodoc.mock import mock
22+
from sphinx.ext.napoleon.docstring import GoogleDocstring
2223
from sphinx.util import logging
2324
from sphinx.util.inspect import signature as sphinx_signature
2425
from sphinx.util.inspect import stringify_signature
@@ -808,6 +809,18 @@ def fix_autodoc_typehints_for_overloaded_methods() -> None:
808809
del MethodDocumenter.format_signature
809810

810811

812+
def patched_lookup_annotation(*_args: Any) -> str: # noqa: U101
813+
"""GoogleDocstring._lookup_annotation sometimes adds incorrect type
814+
annotations to constructor parameters (and otherwise does nothing). Disable
815+
it so we can handle this on our own.
816+
"""
817+
return ""
818+
819+
820+
def patch_google_docstring_lookup_annotation() -> None:
821+
GoogleDocstring._lookup_annotation = patched_lookup_annotation # type: ignore[assignment]
822+
823+
811824
def setup(app: Sphinx) -> dict[str, bool]:
812825
app.add_config_value("always_document_param_types", False, "html")
813826
app.add_config_value("typehints_fully_qualified", False, "env")
@@ -823,6 +836,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
823836
app.connect("autodoc-process-docstring", process_docstring)
824837
fix_autodoc_typehints_for_overloaded_methods()
825838
patch_attribute_handling(app)
839+
patch_google_docstring_lookup_annotation()
826840
return {"parallel_read_safe": True}
827841

828842

tests/test_integration.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from io import StringIO
66
from mailbox import Mailbox
77
from pathlib import Path
8-
from textwrap import dedent
9-
from types import CodeType
8+
from textwrap import dedent, indent
9+
from types import CodeType, ModuleType
1010
from typing import Any, Callable, Optional, TypeVar, Union, overload
1111

1212
import pytest
@@ -909,6 +909,38 @@ def decorator_2(f: Any) -> Any:
909909
f
910910

911911

912+
@expected(
913+
"""
914+
class mod.ParamAndAttributeHaveSameName(blah)
915+
916+
A Class
917+
918+
Parameters:
919+
**blah** ("CodeType") -- Description of parameter blah
920+
921+
blah: "ModuleType"
922+
923+
Description of attribute blah
924+
925+
"""
926+
)
927+
class ParamAndAttributeHaveSameName:
928+
"""
929+
A Class
930+
931+
Parameters
932+
----------
933+
blah:
934+
Description of parameter blah
935+
"""
936+
937+
def __init__(self, blah: CodeType): # noqa: U100
938+
pass
939+
940+
blah: ModuleType
941+
"""Description of attribute blah"""
942+
943+
912944
AUTO_FUNCTION = ".. autofunction:: mod.{}"
913945
AUTO_CLASS = """\
914946
.. autoclass:: mod.{}
@@ -949,5 +981,6 @@ def test_integration(app: SphinxTestApp, status: StringIO, warning: StringIO, mo
949981
try:
950982
assert result.strip() == dedent(expected).strip()
951983
except Exception:
952-
print(f"Result was:\n{result}\n\n")
984+
indented = indent(f'"""\n{result}\n"""', " " * 4)
985+
print(f"@expected(\n{indented}\n)\n")
953986
raise

0 commit comments

Comments
 (0)