Skip to content

Commit 81d4e1b

Browse files
authored
ENH Make types in signature respect typehints_fully_qualified (#400)
Resolves #351, resolves #389
1 parent 92a9bb6 commit 81d4e1b

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

Diff for: src/sphinx_autodoc_typehints/__init__.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,16 @@ def process_signature( # noqa: C901, PLR0913
367367
start = 1
368368

369369
sph_signature = sph_signature.replace(parameters=parameters[start:])
370-
if not app.config.typehints_use_signature_return:
371-
sph_signature = sph_signature.replace(return_annotation=inspect.Signature.empty)
372-
373-
return stringify_signature(sph_signature).replace("\\", "\\\\"), None
370+
show_return_annotation = app.config.typehints_use_signature_return
371+
unqualified_typehints = not getattr(app.config, "typehints_fully_qualified", False)
372+
return (
373+
stringify_signature(
374+
sph_signature,
375+
show_return_annotation=show_return_annotation,
376+
unqualified_typehints=unqualified_typehints,
377+
).replace("\\", "\\\\"),
378+
None,
379+
)
374380

375381

376382
def _is_dataclass(name: str, what: str, qualname: str) -> bool:

Diff for: tests/test_integration.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
from inspect import isclass
77
from pathlib import Path
88
from textwrap import dedent, indent
9-
from typing import TYPE_CHECKING, Any, Callable, NewType, Optional, TypeVar, Union, overload # no type comments
9+
from typing import ( # no type comments
10+
TYPE_CHECKING,
11+
Any,
12+
Callable,
13+
NewType,
14+
Optional,
15+
TypeVar,
16+
Union,
17+
overload,
18+
)
1019

1120
import pytest
1221

1322
if TYPE_CHECKING:
23+
from collections.abc import AsyncGenerator
1424
from io import StringIO
1525
from mailbox import Mailbox
1626
from types import CodeType, ModuleType
@@ -21,9 +31,10 @@
2131
W = NewType("W", str)
2232

2333

24-
def expected(expected: str) -> Callable[[T], T]:
34+
def expected(expected: str, **options: dict[str, Any]) -> Callable[[T], T]:
2535
def dec(val: T) -> T:
2636
val.EXPECTED = expected
37+
val.OPTIONS = options
2738
return val
2839

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

12361247

1248+
@expected(
1249+
"""
1250+
mod.typehints_use_signature(a: AsyncGenerator) -> AsyncGenerator
1251+
1252+
Do something.
1253+
1254+
Parameters:
1255+
**a** ("AsyncGenerator") -- blah
1256+
1257+
Return type:
1258+
"AsyncGenerator"
1259+
1260+
""",
1261+
typehints_use_signature=True,
1262+
typehints_use_signature_return=True,
1263+
)
1264+
def typehints_use_signature(a: AsyncGenerator) -> AsyncGenerator:
1265+
"""Do something.
1266+
1267+
Args:
1268+
a: blah
1269+
"""
1270+
return a
1271+
1272+
12371273
@pytest.mark.parametrize("val", [x for x in globals().values() if hasattr(x, "EXPECTED")])
12381274
@pytest.mark.sphinx("text", testroot="integration")
12391275
def test_integration(
@@ -1251,6 +1287,7 @@ def test_integration(
12511287
template = AUTO_FUNCTION
12521288

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

0 commit comments

Comments
 (0)