|
10 | 10 | import textwrap
|
11 | 11 | import types
|
12 | 12 | 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 |
14 | 14 |
|
15 | 15 | from docutils import nodes
|
16 | 16 | from docutils.frontend import get_default_settings
|
17 | 17 | from sphinx.ext.autodoc.mock import mock
|
18 | 18 | from sphinx.parsers import RSTParser
|
19 | 19 | 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 |
21 | 21 | from sphinx.util.inspect import signature as sphinx_signature
|
22 | 22 |
|
23 | 23 | from ._parser import parse
|
|
61 | 61 | _TYPES_DICT[types.FunctionType] = "FunctionType"
|
62 | 62 |
|
63 | 63 |
|
| 64 | +class MyTypeAliasForwardRef(TypeAliasForwardRef): |
| 65 | + def __or__(self, value: Any) -> Any: |
| 66 | + return Union[self, value] # noqa: UP007 |
| 67 | + |
| 68 | + |
64 | 69 | def _get_types_type(obj: Any) -> str | None:
|
65 | 70 | try:
|
66 | 71 | return _TYPES_DICT.get(obj)
|
@@ -446,7 +451,7 @@ def _future_annotations_imported(obj: Any) -> bool:
|
446 | 451 |
|
447 | 452 |
|
448 | 453 | 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] |
450 | 455 | ) -> dict[str, Any]:
|
451 | 456 | result = _get_type_hint(autodoc_mock_imports, name, obj, localns)
|
452 | 457 | if not result:
|
@@ -517,7 +522,9 @@ def _resolve_type_guarded_imports(autodoc_mock_imports: list[str], obj: Any) ->
|
517 | 522 | _execute_guarded_code(autodoc_mock_imports, obj, module_code)
|
518 | 523 |
|
519 | 524 |
|
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]: |
521 | 528 | _resolve_type_guarded_imports(autodoc_mock_imports, obj)
|
522 | 529 | try:
|
523 | 530 | result = get_type_hints(obj, None, localns)
|
@@ -689,7 +696,7 @@ def process_docstring( # noqa: PLR0913, PLR0917
|
689 | 696 | except (ValueError, TypeError):
|
690 | 697 | signature = None
|
691 | 698 |
|
692 |
| - localns = TypeAliasNamespace(app.config["autodoc_type_aliases"]) |
| 699 | + localns = {key: MyTypeAliasForwardRef(value) for key, value in app.config["autodoc_type_aliases"].items()} |
693 | 700 | type_hints = get_all_type_hints(app.config.autodoc_mock_imports, obj, name, localns)
|
694 | 701 | app.config._annotation_globals = getattr(obj, "__globals__", {}) # noqa: SLF001
|
695 | 702 | try:
|
|
0 commit comments