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