Skip to content

Commit

Permalink
ENH Make types in signature respect typehints_fully_qualified (#400)
Browse files Browse the repository at this point in the history
Resolves #351, resolves #389
  • Loading branch information
hoodmane authored Nov 10, 2023
1 parent 92a9bb6 commit 81d4e1b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,16 @@ def process_signature( # noqa: C901, PLR0913
start = 1

sph_signature = sph_signature.replace(parameters=parameters[start:])
if not app.config.typehints_use_signature_return:
sph_signature = sph_signature.replace(return_annotation=inspect.Signature.empty)

return stringify_signature(sph_signature).replace("\\", "\\\\"), None
show_return_annotation = app.config.typehints_use_signature_return
unqualified_typehints = not getattr(app.config, "typehints_fully_qualified", False)
return (
stringify_signature(
sph_signature,
show_return_annotation=show_return_annotation,
unqualified_typehints=unqualified_typehints,
).replace("\\", "\\\\"),
None,
)


def _is_dataclass(name: str, what: str, qualname: str) -> bool:
Expand Down
41 changes: 39 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
from inspect import isclass
from pathlib import Path
from textwrap import dedent, indent
from typing import TYPE_CHECKING, Any, Callable, NewType, Optional, TypeVar, Union, overload # no type comments
from typing import ( # no type comments
TYPE_CHECKING,
Any,
Callable,
NewType,
Optional,
TypeVar,
Union,
overload,
)

import pytest

if TYPE_CHECKING:
from collections.abc import AsyncGenerator
from io import StringIO
from mailbox import Mailbox
from types import CodeType, ModuleType
Expand All @@ -21,9 +31,10 @@
W = NewType("W", str)


def expected(expected: str) -> Callable[[T], T]:
def expected(expected: str, **options: dict[str, Any]) -> Callable[[T], T]:
def dec(val: T) -> T:
val.EXPECTED = expected
val.OPTIONS = options
return val

return dec
Expand Down Expand Up @@ -1234,6 +1245,31 @@ def has_newtype(param: W) -> W:
LT_PY310 = sys.version_info < (3, 10)


@expected(
"""
mod.typehints_use_signature(a: AsyncGenerator) -> AsyncGenerator
Do something.
Parameters:
**a** ("AsyncGenerator") -- blah
Return type:
"AsyncGenerator"
""",
typehints_use_signature=True,
typehints_use_signature_return=True,
)
def typehints_use_signature(a: AsyncGenerator) -> AsyncGenerator:
"""Do something.
Args:
a: blah
"""
return a


@pytest.mark.parametrize("val", [x for x in globals().values() if hasattr(x, "EXPECTED")])
@pytest.mark.sphinx("text", testroot="integration")
def test_integration(
Expand All @@ -1251,6 +1287,7 @@ def test_integration(
template = AUTO_FUNCTION

(Path(app.srcdir) / "index.rst").write_text(template.format(val.__name__))
app.config.__dict__.update(val.OPTIONS)
monkeypatch.setitem(sys.modules, "mod", sys.modules[__name__])
app.build()
assert "build succeeded" in status.getvalue() # Build succeeded
Expand Down

0 comments on commit 81d4e1b

Please sign in to comment.