Skip to content

Commit 7a9f674

Browse files
♻️ refactor(tests): fold per-issue test files into topical ones (#715)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a03875e commit 7a9f674

19 files changed

Lines changed: 472 additions & 711 deletions

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ lint.per-file-ignores."tests/**/*.py" = [
122122
"UP007", # we test for old Union syntax
123123
"UP045", # we test for old Optional syntax
124124
]
125-
lint.per-file-ignores."tests/roots/test-issue_572/mod_forward_ref.py" = [
125+
lint.per-file-ignores."tests/roots/test-integration/mod_eager.py" = [
126+
"I002", # this file intentionally omits `from __future__ import annotations` to test non-deferred annotations
127+
]
128+
lint.per-file-ignores."tests/roots/test-integration/mod_forward_ref.py" = [
126129
"I002", # intentionally omits `from __future__ import annotations` to test forward references
127130
"PLR6301", # method must be instance method to test class forward refs
128131
]
129-
lint.per-file-ignores."tests/roots/test-issue_599/mod_eager.py" = [
130-
"I002", # this file intentionally omits `from __future__ import annotations` to test non-deferred annotations
131-
]
132132
lint.per-file-ignores."tests/roots/test-pyi-stubs/stub_mod.py" = [
133133
"ANN", # intentionally has no type annotations to simulate a C extension
134134
"I002", # intentionally omits `from __future__ import annotations`
File renamed without changes.

tests/roots/test-issue_572/conf.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/roots/test-issue_572/index.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/roots/test-issue_599/conf.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/roots/test-issue_599/index.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.
File renamed without changes.

tests/test_annotations.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@
2929
TypeVar,
3030
Union,
3131
)
32-
from unittest.mock import create_autospec
32+
from unittest.mock import MagicMock, create_autospec
3333

3434
import pytest
3535
import typing_extensions
3636
from sphinx.config import Config
37+
from sphinx.util.inspect import TypeAliasForwardRef
3738

3839
from sphinx_autodoc_typehints import (
3940
format_annotation,
4041
get_annotation_args,
4142
get_annotation_class_name,
4243
get_annotation_module,
4344
)
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
4546

4647
if TYPE_CHECKING:
4748
from sphobjinv import Inventory
@@ -606,3 +607,44 @@ def test_get_canonical_type_alias_name_no_public_reexport(monkeypatch: pytest.Mo
606607
alias: TypeAliasType = priv.__dict__["ExtAlias2"]
607608
monkeypatch.setitem(sys.modules, "extpkg2._priv", priv)
608609
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

Comments
 (0)