Skip to content

Commit 05b79e3

Browse files
nineteendoWannes Boeykenspre-commit-ci[bot]
authored
Fix issue #481 (#533)
Co-authored-by: Wannes Boeykens <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a003dfc commit 05b79e3

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import textwrap
1111
import types
1212
from dataclasses import dataclass
13-
from typing import TYPE_CHECKING, Any, AnyStr, ForwardRef, NewType, TypeVar, get_type_hints
13+
from typing import TYPE_CHECKING, Any, AnyStr, ForwardRef, NewType, TypeVar, Union, get_type_hints
1414

1515
from docutils import nodes
1616
from docutils.frontend import get_default_settings
1717
from sphinx.ext.autodoc.mock import mock
1818
from sphinx.parsers import RSTParser
1919
from sphinx.util import logging, rst
20-
from sphinx.util.inspect import TypeAliasForwardRef, TypeAliasNamespace, stringify_signature
20+
from sphinx.util.inspect import TypeAliasForwardRef, stringify_signature
2121
from sphinx.util.inspect import signature as sphinx_signature
2222

2323
from ._parser import parse
@@ -61,6 +61,11 @@
6161
_TYPES_DICT[types.FunctionType] = "FunctionType"
6262

6363

64+
class MyTypeAliasForwardRef(TypeAliasForwardRef):
65+
def __or__(self, value: Any) -> Any:
66+
return Union[self, value] # noqa: UP007
67+
68+
6469
def _get_types_type(obj: Any) -> str | None:
6570
try:
6671
return _TYPES_DICT.get(obj)
@@ -446,7 +451,7 @@ def _future_annotations_imported(obj: Any) -> bool:
446451

447452

448453
def get_all_type_hints(
449-
autodoc_mock_imports: list[str], obj: Any, name: str, localns: TypeAliasNamespace
454+
autodoc_mock_imports: list[str], obj: Any, name: str, localns: dict[Any, MyTypeAliasForwardRef]
450455
) -> dict[str, Any]:
451456
result = _get_type_hint(autodoc_mock_imports, name, obj, localns)
452457
if not result:
@@ -517,7 +522,9 @@ def _resolve_type_guarded_imports(autodoc_mock_imports: list[str], obj: Any) ->
517522
_execute_guarded_code(autodoc_mock_imports, obj, module_code)
518523

519524

520-
def _get_type_hint(autodoc_mock_imports: list[str], name: str, obj: Any, localns: TypeAliasNamespace) -> dict[str, Any]:
525+
def _get_type_hint(
526+
autodoc_mock_imports: list[str], name: str, obj: Any, localns: dict[Any, MyTypeAliasForwardRef]
527+
) -> dict[str, Any]:
521528
_resolve_type_guarded_imports(autodoc_mock_imports, obj)
522529
try:
523530
result = get_type_hints(obj, None, localns)
@@ -689,7 +696,7 @@ def process_docstring( # noqa: PLR0913, PLR0917
689696
except (ValueError, TypeError):
690697
signature = None
691698

692-
localns = TypeAliasNamespace(app.config["autodoc_type_aliases"])
699+
localns = {key: MyTypeAliasForwardRef(value) for key, value in app.config["autodoc_type_aliases"].items()}
693700
type_hints = get_all_type_hints(app.config.autodoc_mock_imports, obj, name, localns)
694701
app.config._annotation_globals = getattr(obj, "__globals__", {}) # noqa: SLF001
695702
try:

tests/test_integration_autodoc_type_aliases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def g(s: AliasedClass) -> AliasedClass:
103103
Function docstring.
104104
105105
Parameters:
106-
* **x** (Array) -- foo
106+
* **x** ("Optional"[Array]) -- foo
107107
108108
* **y** ("Schema") -- boo
109109
@@ -115,7 +115,7 @@ def g(s: AliasedClass) -> AliasedClass:
115115
116116
""",
117117
)
118-
def function(x: ArrayLike, y: Schema) -> str:
118+
def function(x: ArrayLike | None, y: Schema) -> str:
119119
"""
120120
Function docstring.
121121

0 commit comments

Comments
 (0)