|
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 | 21 | from sphinx.util.inspect import signature as sphinx_signature
|
21 |
| -from sphinx.util.inspect import stringify_signature |
22 | 22 |
|
23 | 23 | from .parser import parse
|
24 | 24 | from .patches import install_patches
|
@@ -194,6 +194,9 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
|
194 | 194 | if isinstance(annotation, tuple):
|
195 | 195 | return format_internal_tuple(annotation, config)
|
196 | 196 |
|
| 197 | + if isinstance(annotation, TypeAliasForwardRef): |
| 198 | + return str(annotation) |
| 199 | + |
197 | 200 | try:
|
198 | 201 | module = get_annotation_module(annotation)
|
199 | 202 | class_name = get_annotation_class_name(annotation, module)
|
@@ -404,16 +407,18 @@ def _future_annotations_imported(obj: Any) -> bool:
|
404 | 407 | return bool(_annotations.compiler_flag == future_annotations)
|
405 | 408 |
|
406 | 409 |
|
407 |
| -def get_all_type_hints(autodoc_mock_imports: list[str], obj: Any, name: str) -> dict[str, Any]: |
408 |
| - result = _get_type_hint(autodoc_mock_imports, name, obj) |
| 410 | +def get_all_type_hints( |
| 411 | + autodoc_mock_imports: list[str], obj: Any, name: str, localns: TypeAliasNamespace |
| 412 | +) -> dict[str, Any]: |
| 413 | + result = _get_type_hint(autodoc_mock_imports, name, obj, localns) |
409 | 414 | if not result:
|
410 | 415 | result = backfill_type_hints(obj, name)
|
411 | 416 | try:
|
412 | 417 | obj.__annotations__ = result
|
413 | 418 | except (AttributeError, TypeError):
|
414 | 419 | pass
|
415 | 420 | else:
|
416 |
| - result = _get_type_hint(autodoc_mock_imports, name, obj) |
| 421 | + result = _get_type_hint(autodoc_mock_imports, name, obj, localns) |
417 | 422 | return result
|
418 | 423 |
|
419 | 424 |
|
@@ -474,10 +479,10 @@ def _resolve_type_guarded_imports(autodoc_mock_imports: list[str], obj: Any) ->
|
474 | 479 | _execute_guarded_code(autodoc_mock_imports, obj, module_code)
|
475 | 480 |
|
476 | 481 |
|
477 |
| -def _get_type_hint(autodoc_mock_imports: list[str], name: str, obj: Any) -> dict[str, Any]: |
| 482 | +def _get_type_hint(autodoc_mock_imports: list[str], name: str, obj: Any, localns: TypeAliasNamespace) -> dict[str, Any]: |
478 | 483 | _resolve_type_guarded_imports(autodoc_mock_imports, obj)
|
479 | 484 | try:
|
480 |
| - result = get_type_hints(obj) |
| 485 | + result = get_type_hints(obj, None, localns) |
481 | 486 | except (AttributeError, TypeError, RecursionError) as exc:
|
482 | 487 | # TypeError - slot wrapper, PEP-563 when part of new syntax not supported
|
483 | 488 | # RecursionError - some recursive type definitions https://github.com/python/typing/issues/574
|
@@ -645,7 +650,9 @@ def process_docstring( # noqa: PLR0913, PLR0917
|
645 | 650 | signature = sphinx_signature(obj, type_aliases=app.config["autodoc_type_aliases"])
|
646 | 651 | except (ValueError, TypeError):
|
647 | 652 | signature = None
|
648 |
| - type_hints = get_all_type_hints(app.config.autodoc_mock_imports, obj, name) |
| 653 | + |
| 654 | + localns = TypeAliasNamespace(app.config["autodoc_type_aliases"]) |
| 655 | + type_hints = get_all_type_hints(app.config.autodoc_mock_imports, obj, name, localns) |
649 | 656 | app.config._annotation_globals = getattr(obj, "__globals__", {}) # type: ignore[attr-defined] # noqa: SLF001
|
650 | 657 | try:
|
651 | 658 | _inject_types_to_docstring(type_hints, signature, original_obj, app, what, name, lines)
|
@@ -715,10 +722,8 @@ def _inject_signature( # noqa: C901
|
715 | 722 | app: Sphinx,
|
716 | 723 | lines: list[str],
|
717 | 724 | ) -> None:
|
718 |
| - type_aliases = app.config["autodoc_type_aliases"] |
719 |
| - |
720 |
| - for arg_name, arg_type in signature.parameters.items(): |
721 |
| - annotation = arg_type.annotation if arg_type.annotation in type_aliases else type_hints.get(arg_name) |
| 725 | + for arg_name in signature.parameters: |
| 726 | + annotation = type_hints.get(arg_name) |
722 | 727 |
|
723 | 728 | default = signature.parameters[arg_name].default
|
724 | 729 |
|
|
0 commit comments