|
29 | 29 | TypeVar, |
30 | 30 | Union, |
31 | 31 | ) |
32 | | -from unittest.mock import create_autospec |
| 32 | +from unittest.mock import MagicMock, create_autospec |
33 | 33 |
|
34 | 34 | import pytest |
35 | 35 | import typing_extensions |
36 | 36 | from sphinx.config import Config |
| 37 | +from sphinx.util.inspect import TypeAliasForwardRef |
37 | 38 |
|
38 | 39 | from sphinx_autodoc_typehints import ( |
39 | 40 | format_annotation, |
40 | 41 | get_annotation_args, |
41 | 42 | get_annotation_class_name, |
42 | 43 | get_annotation_module, |
43 | 44 | ) |
44 | | -from sphinx_autodoc_typehints._annotations import _get_canonical_type_alias_name |
| 45 | +from sphinx_autodoc_typehints._annotations import MyTypeAliasForwardRef, _get_canonical_type_alias_name |
45 | 46 |
|
46 | 47 | if TYPE_CHECKING: |
47 | 48 | from sphobjinv import Inventory |
@@ -606,3 +607,44 @@ def test_get_canonical_type_alias_name_no_public_reexport(monkeypatch: pytest.Mo |
606 | 607 | alias: TypeAliasType = priv.__dict__["ExtAlias2"] |
607 | 608 | monkeypatch.setitem(sys.modules, "extpkg2._priv", priv) |
608 | 609 | assert _get_canonical_type_alias_name(alias) == "extpkg2._priv.ExtAlias2" |
| 610 | + |
| 611 | + |
| 612 | +def _config_without_env(*, fully_qualified: bool = False) -> MagicMock: |
| 613 | + config = MagicMock() |
| 614 | + config.typehints_formatter = None |
| 615 | + config.typehints_fully_qualified = fully_qualified |
| 616 | + del config._typehints_env # noqa: SLF001 |
| 617 | + return config |
| 618 | + |
| 619 | + |
| 620 | +def test_format_annotation_type_alias_without_env() -> None: |
| 621 | + """TypeAliasForwardRef falls back to plain name when no env is available.""" |
| 622 | + assert format_annotation(TypeAliasForwardRef("SomeAlias"), _config_without_env()) == "SomeAlias" |
| 623 | + |
| 624 | + |
| 625 | +@pytest.mark.parametrize( |
| 626 | + ("crossref", "fully_qualified", "expected_result"), |
| 627 | + [ |
| 628 | + pytest.param(True, False, ":py:type:`~EncoderHook`", id="crossref"), |
| 629 | + pytest.param(True, True, ":py:type:`EncoderHook`", id="crossref-fully-qualified"), |
| 630 | + pytest.param(False, False, "EncoderHook", id="plain-name"), |
| 631 | + ], |
| 632 | +) |
| 633 | +def test_format_annotation_crossref_alias(crossref: bool, fully_qualified: bool, expected_result: str) -> None: |
| 634 | + annotation = MyTypeAliasForwardRef("EncoderHook") |
| 635 | + annotation.crossref = crossref |
| 636 | + assert format_annotation(annotation, _config_without_env(fully_qualified=fully_qualified)) == expected_result |
| 637 | + |
| 638 | + |
| 639 | +def test_format_annotation_type_alias_found_in_py_domain() -> None: |
| 640 | + config = MagicMock() |
| 641 | + config.typehints_formatter = None |
| 642 | + config.typehints_fully_qualified = False |
| 643 | + config._typehints_module_prefix = "mymod" # noqa: SLF001 |
| 644 | + py_domain = MagicMock() |
| 645 | + py_domain.objects = {"mymod.SomeAlias": MagicMock(objtype="type")} |
| 646 | + env = MagicMock() |
| 647 | + env.get_domain.return_value = py_domain |
| 648 | + config._typehints_env = env # noqa: SLF001 |
| 649 | + annotation = TypeAliasForwardRef("SomeAlias") |
| 650 | + assert format_annotation(annotation, config) == ":py:type:`~mymod.SomeAlias`" |
0 commit comments