Debian Codegraph @

tests/test_annotations.py

raw symbols SCIP
1 import inspect
2 from collections import namedtuple
3 from collections.abc import Iterable, Sequence
4 from pathlib import Path
5 from typing import Annotated, Any, Literal, Optional, Union
6
7 import pytest
8
9 from cyclopts.annotations import contains_hint, get_hint_name, is_iterable_type, resolve
10
11
12 def test_resolve_annotated():
13 type_ = Annotated[Literal["foo", "bar"], "fizz"]
14 res = resolve(type_)
15 assert res == Literal["foo", "bar"]
16
17
18 def test_resolve_empty():
19 res = resolve(inspect.Parameter.empty)
20 assert res is str
21
22
23 def test_get_hint_name_string():
24 assert get_hint_name("str") == "str"
25
26
27 def test_get_hint_name_any():
28 assert get_hint_name(Any) == "Any"
29
30
31 def test_get_hint_name_union():
32 assert get_hint_name(Union[int, str]) == "int|str"
33
34
35 def test_get_hint_name_class_with_name():
36 class TestClass:
37 pass
38
39 assert get_hint_name(TestClass) == "TestClass"
40
41
42 def test_get_hint_name_typing_with_name():
43 assert get_hint_name(list) == "list"
44
45
46 def test_get_hint_name_generic_type():
47 assert get_hint_name(list[int]) == "list[int]"
48
49
50 def test_get_hint_name_nested_generic_type():
51 assert get_hint_name(dict[str, list[int]]) == "dict[str, list[int]]"
52
53
54 def test_get_hint_name_optional_type():
55 assert get_hint_name(Optional[int]) == "int|None"
56
57
58 def test_get_hint_name_namedtuple():
59 TestTuple = namedtuple("TestTuple", ["field1", "field2"])
60 assert get_hint_name(TestTuple) == "TestTuple"
61
62
63 def test_get_hint_name_complex_union():
64 complex_type = Union[int, str, list[dict[str, Any]]]
65 assert get_hint_name(complex_type) == "int|str|list[dict[str, Any]]"
66
67
68 def test_get_hint_name_fallback_str():
69 class NoNameClass:
70 def __str__(self):
71 return "NoNameClass"
72
73 assert get_hint_name(NoNameClass()) == "NoNameClass"
74
75
76 class CustomStr(str):
77 """Dummy subclass of ``str``."""
78
79
80 @pytest.mark.parametrize(
81 "hint,target_type,expected",
82 [
83 (str, str, True),
84 (CustomStr, str, True),
85 (Union[int, str], str, True),
86 (Annotated[int | str, 1], str, True),
87 (Annotated[Annotated[int, 1] | Annotated[str, 1], 1], str, True),
88 (int, str, False),
89 ],
90 )
91 def test_contains_hint(hint, target_type, expected):
92 assert contains_hint(hint, target_type) == expected
93
94
95 @pytest.mark.parametrize(
96 "hint,expected",
97 [
98 # Basic collection types
99 (list[str], True),
100 (set[int], True),
101 (tuple[str, ...], True),
102 (frozenset[str], True),
103 # Abstract collection types
104 (Sequence[str], True),
105 (Iterable[str], True),
106 # Annotated wrappers
107 (Annotated[list[str], "metadata"], True),
108 # Optional wrappers
109 (Optional[list[str]], True),
110 # Nested Annotated inside collection
111 (list[Annotated[Path, "metadata"]], True),
112 # Non-iterable types
113 (str, False),
114 (int, False),
115 (Path, False),
116 (dict[str, str], True),
117 (Annotated[str, "metadata"], False),
118 (Optional[str], False),
119 ],
120 )
121 def test_is_iterable_type(hint, expected):
122 assert is_iterable_type(hint) == expected
123
124
125 def test_get_annotated_discriminator_rejects_non_annotated():
126 from cyclopts.annotations import get_annotated_discriminator
127
128 class Decoy:
129 discriminator = "type"
130
131 assert get_annotated_discriminator(list[Decoy]) is None
132 assert get_annotated_discriminator(int) is None
133
134
135 def test_get_annotated_discriminator_positive():
136 from types import SimpleNamespace
137
138 from cyclopts.annotations import get_annotated_discriminator
139
140 assert get_annotated_discriminator(Annotated[int, SimpleNamespace(discriminator="type")]) == "type"