1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import inspect
from collections import namedtuple
from typing import Annotated, Any, Dict, List, Literal, Optional, Union
import pytest
from cyclopts.annotations import contains_hint, get_hint_name, resolve
def test_resolve_annotated():
type_ = Annotated[Literal["foo", "bar"], "fizz"]
res = resolve(type_)
assert res == Literal["foo", "bar"]
def test_resolve_empty():
res = resolve(inspect.Parameter.empty)
assert res is str
def test_get_hint_name_string():
assert get_hint_name("str") == "str"
def test_get_hint_name_any():
assert get_hint_name(Any) == "Any"
def test_get_hint_name_union():
assert get_hint_name(Union[int, str]) == "int|str"
def test_get_hint_name_class_with_name():
class TestClass:
pass
assert get_hint_name(TestClass) == "TestClass"
def test_get_hint_name_typing_with_name():
assert get_hint_name(List) == "list"
def test_get_hint_name_generic_type():
assert get_hint_name(List[int]) == "list[int]"
def test_get_hint_name_nested_generic_type():
assert get_hint_name(Dict[str, List[int]]) == "dict[str, list[int]]"
def test_get_hint_name_optional_type():
assert get_hint_name(Optional[int]) == "int|None"
def test_get_hint_name_namedtuple():
TestTuple = namedtuple("TestTuple", ["field1", "field2"])
assert get_hint_name(TestTuple) == "TestTuple"
def test_get_hint_name_complex_union():
complex_type = Union[int, str, List[Dict[str, Any]]]
assert get_hint_name(complex_type) == "int|str|list[dict[str, Any]]"
def test_get_hint_name_fallback_str():
class NoNameClass:
def __str__(self):
return "NoNameClass"
assert get_hint_name(NoNameClass()) == "NoNameClass"
class CustomStr(str):
"""Dummy subclass of ``str``."""
@pytest.mark.parametrize(
"hint,target_type,expected",
[
(str, str, True),
(CustomStr, str, True),
(Union[int, str], str, True),
(Annotated[Union[int, str], 1], str, True),
(Annotated[Union[Annotated[int, 1], Annotated[str, 1]], 1], str, True),
(int, str, False),
],
)
def test_contains_hint(hint, target_type, expected):
assert contains_hint(hint, target_type) == expected
|