Skip to content
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
14 changes: 14 additions & 0 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from sphinx.environment import BuildEnvironment
from sphinx.ext.autodoc import Options
from sphinx.ext.autodoc.mock import mock
from sphinx.ext.napoleon.docstring import GoogleDocstring
from sphinx.util import logging
from sphinx.util.inspect import signature as sphinx_signature
from sphinx.util.inspect import stringify_signature
Expand Down Expand Up @@ -808,6 +809,18 @@ def fix_autodoc_typehints_for_overloaded_methods() -> None:
del MethodDocumenter.format_signature


def patched_lookup_annotation(*_args: Any) -> str: # noqa: U101
"""GoogleDocstring._lookup_annotation sometimes adds incorrect type
annotations to constructor parameters (and otherwise does nothing). Disable
it so we can handle this on our own.
"""
return ""


def patch_google_docstring_lookup_annotation() -> None:
GoogleDocstring._lookup_annotation = patched_lookup_annotation # type: ignore[assignment]


def setup(app: Sphinx) -> dict[str, bool]:
app.add_config_value("always_document_param_types", False, "html")
app.add_config_value("typehints_fully_qualified", False, "env")
Expand All @@ -823,6 +836,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
app.connect("autodoc-process-docstring", process_docstring)
fix_autodoc_typehints_for_overloaded_methods()
patch_attribute_handling(app)
patch_google_docstring_lookup_annotation()
return {"parallel_read_safe": True}


Expand Down
39 changes: 36 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from io import StringIO
from mailbox import Mailbox
from pathlib import Path
from textwrap import dedent
from types import CodeType
from textwrap import dedent, indent
from types import CodeType, ModuleType
from typing import Any, Callable, Optional, TypeVar, Union, overload

import pytest
Expand Down Expand Up @@ -909,6 +909,38 @@ def decorator_2(f: Any) -> Any:
f


@expected(
"""
class mod.ParamAndAttributeHaveSameName(blah)

A Class

Parameters:
**blah** ("CodeType") -- Description of parameter blah

blah: "ModuleType"

Description of attribute blah

"""
)
class ParamAndAttributeHaveSameName:
"""
A Class

Parameters
----------
blah:
Description of parameter blah
"""

def __init__(self, blah: CodeType): # noqa: U100
pass

blah: ModuleType
"""Description of attribute blah"""


AUTO_FUNCTION = ".. autofunction:: mod.{}"
AUTO_CLASS = """\
.. autoclass:: mod.{}
Expand Down Expand Up @@ -949,5 +981,6 @@ def test_integration(app: SphinxTestApp, status: StringIO, warning: StringIO, mo
try:
assert result.strip() == dedent(expected).strip()
except Exception:
print(f"Result was:\n{result}\n\n")
indented = indent(f'"""\n{result}\n"""', " " * 4)
print(f"@expected(\n{indented}\n)\n")
raise