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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
# ruff: noqa: UP007, UP006
from __future__ import annotations
from sys import version_info
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
import pytest
from typing_extensions import Annotated
from litestar.utils.typing import (
expand_type_var_in_type_hint,
get_origin_or_inner_type,
get_type_hints_with_generics_resolved,
make_non_optional_union,
)
from tests.models import DataclassPerson, DataclassPet # noqa: F401
if version_info >= (3, 10):
from collections import deque # noqa: F401
py_310_plus_annotation = [
(eval(tp), exp)
for tp, exp in [
("tuple[DataclassPerson, ...]", True),
("list[DataclassPerson]", True),
("deque[DataclassPerson]", True),
("tuple[DataclassPet, ...]", False),
("list[DataclassPet]", False),
("deque[DataclassPet]", False),
]
]
else:
py_310_plus_annotation = []
@pytest.mark.parametrize(
("annotation", "expected"), [(Union[None, str, int], Union[str, int]), (Optional[Union[str, int]], Union[str, int])]
)
def test_make_non_optional_union(annotation: Any, expected: Any) -> None:
assert make_non_optional_union(annotation) == expected
def test_get_origin_or_inner_type() -> None:
assert get_origin_or_inner_type(List[DataclassPerson]) == list
assert get_origin_or_inner_type(Annotated[List[DataclassPerson], "foo"]) == list
assert get_origin_or_inner_type(Annotated[Dict[str, List[DataclassPerson]], "foo"]) == dict
T = TypeVar("T")
V = TypeVar("V", int, str)
U = TypeVar("U", bound=int)
ANNOTATION = object()
class Foo(Generic[T]):
foo: T
class BoundFoo(Generic[U]):
bound_foo: U
class ConstrainedFoo(Generic[V]):
constrained_foo: V
class AnnotatedFoo(Generic[T]):
annotated_foo: Annotated[T, ANNOTATION]
class UnionFoo(Generic[T, V, U]):
union_foo: Union[T, bool]
constrained_union_foo: Union[V, bool]
bound_union_foo: Union[U, bool]
class MixedFoo(Generic[T]):
foo: T
list_foo: List[T]
normal_foo: str
normal_list_foo: List[str]
class NestedFoo(Generic[T]):
bound_foo: BoundFoo
constrained_foo: ConstrainedFoo
constrained_foo_with_t: ConstrainedFoo[int]
@pytest.mark.parametrize(
("annotation", "expected_type_hints"),
(
(Foo[int], {"foo": int}),
(BoundFoo, {"bound_foo": int}),
(BoundFoo[int], {"bound_foo": int}),
(ConstrainedFoo[int], {"constrained_foo": int}),
(ConstrainedFoo, {"constrained_foo": Union[int, str]}),
(AnnotatedFoo[int], {"annotated_foo": Annotated[int, ANNOTATION]}),
(
UnionFoo[T, V, U], # type: ignore[valid-type]
{
"union_foo": Union[T, bool], # pyright: ignore[reportGeneralTypeIssues]
"constrained_union_foo": Union[int, str, bool],
"bound_union_foo": Union[int, bool],
},
),
(
UnionFoo,
{
"union_foo": Union[T, bool], # pyright: ignore[reportGeneralTypeIssues]
"constrained_union_foo": Union[int, str, bool],
"bound_union_foo": Union[int, bool],
},
),
(
MixedFoo[int],
{
"foo": int,
"list_foo": List[int],
"normal_foo": str,
"normal_list_foo": List[str],
},
),
(
NestedFoo[int],
{
"bound_foo": BoundFoo[int],
"constrained_foo": ConstrainedFoo[Union[int, str]], # type: ignore[type-var]
"constrained_foo_with_t": ConstrainedFoo[int],
},
),
),
)
def test_get_type_hints_with_generics(annotation: Any, expected_type_hints: dict[str, Any]) -> None:
assert get_type_hints_with_generics_resolved(annotation, include_extras=True) == expected_type_hints
class ConcreteT: ...
@pytest.mark.parametrize(
("type_hint", "namespace", "expected"),
(
({"arg1": T, "return": int}, {}, {"arg1": T, "return": int}),
({"arg1": T, "return": int}, None, {"arg1": T, "return": int}),
({"arg1": T, "return": int}, {U: ConcreteT}, {"arg1": T, "return": int}),
({"arg1": T, "return": int}, {T: ConcreteT}, {"arg1": ConcreteT, "return": int}),
({"arg1": T, "return": int}, {T: int}, {"arg1": int, "return": int}),
({"arg1": int, "return": int}, {}, {"arg1": int, "return": int}),
({"arg1": int, "return": int}, None, {"arg1": int, "return": int}),
({"arg1": int, "return": int}, {T: int}, {"arg1": int, "return": int}),
({"arg1": T, "return": T}, {T: ConcreteT}, {"arg1": ConcreteT, "return": ConcreteT}),
({"arg1": T, "return": T}, {T: int}, {"arg1": int, "return": int}),
),
)
def test_expand_type_var_in_type_hints(
type_hint: dict[str, Any], namespace: dict[str, Any] | None, expected: dict[str, Any]
) -> None:
assert expand_type_var_in_type_hint(type_hint, namespace) == expected
|