Skip to content

Commit

Permalink
Disable GoogleDocstring._lookup_annotation (#309)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Resolves #308
  • Loading branch information
hoodmane committed Jan 20, 2023
1 parent 15c6456 commit 53f7f00
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/sphinx_autodoc_typehints/__init__.py
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
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

0 comments on commit 53f7f00

Please sign in to comment.