| 1 |
import sys |
| 2 |
import typing as t |
| 3 |
from dataclasses import InitVar |
| 4 |
from textwrap import dedent |
| 5 |
from typing import Any, Literal |
| 6 |
|
| 7 |
import pytest |
| 8 |
import typing_extensions as t_e |
| 9 |
|
| 10 |
from typing_inspection.introspection import UNKNOWN, AnnotationSource, ForbiddenQualifier, inspect_annotation |
| 11 |
|
| 12 |
|
| 13 |
def test_unknown_repr() -> None: |
| 14 |
assert str(UNKNOWN) == 'UNKNOWN' |
| 15 |
assert repr(UNKNOWN) == '<UNKNOWN>' |
| 16 |
|
| 17 |
|
| 18 |
_all_qualifiers: list[Any] = [ |
| 19 |
t_e.ClassVar[int], |
| 20 |
t_e.Final[int], |
| 21 |
t_e.ReadOnly[int], |
| 22 |
t_e.Required[int], |
| 23 |
t_e.NotRequired[int], |
| 24 |
InitVar[int], |
| 25 |
] |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
['source', 'annotations'], |
| 30 |
[ |
| 31 |
(AnnotationSource.ASSIGNMENT_OR_VARIABLE, [t_e.Final[int]]), |
| 32 |
(AnnotationSource.CLASS, [t_e.ClassVar[int], t.Final[int]]), |
| 33 |
(AnnotationSource.DATACLASS, [t_e.ClassVar[int], t.Final[int], InitVar[int]]), |
| 34 |
(AnnotationSource.TYPED_DICT, [t_e.ReadOnly[int], t_e.Required[int], t_e.NotRequired[int]]), |
| 35 |
(AnnotationSource.ANY, _all_qualifiers), |
| 36 |
], |
| 37 |
) |
| 38 |
def test_annotation_source_valid_qualifiers(source: AnnotationSource, annotations: list[Any]) -> None: |
| 39 |
for annotation in annotations: |
| 40 |
assert inspect_annotation(annotation, annotation_source=source).type is int |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize( |
| 44 |
['source', 'annotations'], |
| 45 |
[ |
| 46 |
( |
| 47 |
AnnotationSource.ASSIGNMENT_OR_VARIABLE, |
| 48 |
[t_e.ClassVar[int], t_e.ReadOnly[int], t_e.Required[int], t_e.NotRequired[int], InitVar[int]], |
| 49 |
), |
| 50 |
(AnnotationSource.CLASS, [t_e.ReadOnly[int], t_e.Required[int], t_e.NotRequired[int], InitVar[int]]), |
| 51 |
(AnnotationSource.DATACLASS, [t_e.ReadOnly[int], t_e.Required[int], t_e.NotRequired[int]]), |
| 52 |
(AnnotationSource.TYPED_DICT, [t_e.ClassVar[int], t_e.Final[int], InitVar[int]]), |
| 53 |
(AnnotationSource.NAMED_TUPLE, _all_qualifiers), |
| 54 |
(AnnotationSource.FUNCTION, _all_qualifiers), |
| 55 |
(AnnotationSource.BARE, _all_qualifiers), |
| 56 |
], |
| 57 |
) |
| 58 |
def test_annotation_source_invalid_qualifiers(source: AnnotationSource, annotations: list[Any]) -> None: |
| 59 |
for annotation in annotations: |
| 60 |
with pytest.raises(ForbiddenQualifier): |
| 61 |
inspect_annotation(annotation, annotation_source=source) |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.parametrize( |
| 65 |
['qualifier_obj', 'qualifier_str'], |
| 66 |
[ |
| 67 |
(t.Final, 'final'), |
| 68 |
(t.ClassVar, 'class_var'), |
| 69 |
(InitVar, 'init_var'), |
| 70 |
], |
| 71 |
) |
| 72 |
def test_bare_qualifier(qualifier_obj: Any, qualifier_str: str) -> None: |
| 73 |
result = inspect_annotation( |
| 74 |
qualifier_obj, |
| 75 |
annotation_source=AnnotationSource.ANY, |
| 76 |
) |
| 77 |
|
| 78 |
assert result.qualifiers == {qualifier_str} |
| 79 |
assert result.type is UNKNOWN |
| 80 |
|
| 81 |
with pytest.raises(ForbiddenQualifier): |
| 82 |
inspect_annotation(qualifier_obj, annotation_source=AnnotationSource.BARE) |
| 83 |
|
| 84 |
|
| 85 |
def test_nested_metadata_and_qualifiers() -> None: |
| 86 |
result = inspect_annotation( |
| 87 |
t_e.Final[t_e.Annotated[t_e.ClassVar[t_e.Annotated[int, 1]], 2]], # pyright: ignore[reportInvalidTypeForm] |
| 88 |
annotation_source=AnnotationSource.ANY, |
| 89 |
) |
| 90 |
|
| 91 |
assert result.type is int |
| 92 |
assert result.qualifiers == {'class_var', 'final'} |
| 93 |
assert result.metadata == [1, 2] |
| 94 |
|
| 95 |
|
| 96 |
Alias1 = t_e.TypeAliasType('Alias1', int) |
| 97 |
Alias2 = t_e.TypeAliasType('Alias2', t_e.Annotated[t_e.ReadOnly[Alias1], 1]) # pyright: ignore[reportInvalidTypeForm] |
| 98 |
Alias3 = t_e.TypeAliasType('Alias3', t_e.Annotated[t_e.Required[Alias2], 2]) # pyright: ignore[reportInvalidTypeForm] |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize( |
| 102 |
'unpack_mode', |
| 103 |
['eager', 'lenient'], |
| 104 |
) |
| 105 |
def test_unpack_type_aliases(unpack_mode: Literal['eager', 'lenient']) -> None: |
| 106 |
result = inspect_annotation( |
| 107 |
t_e.Final[t_e.Annotated[t_e.ClassVar[t_e.Annotated[Alias3, 3]], 4]], # pyright: ignore[reportInvalidTypeForm] |
| 108 |
annotation_source=AnnotationSource.ANY, |
| 109 |
unpack_type_aliases=unpack_mode, |
| 110 |
) |
| 111 |
|
| 112 |
assert result.type is Alias1 |
| 113 |
assert result.qualifiers == {'read_only', 'required', 'class_var', 'final'} |
| 114 |
assert result.metadata == [1, 2, 3, 4] |
| 115 |
|
| 116 |
|
| 117 |
T = t.TypeVar('T') |
| 118 |
|
| 119 |
FlatList = t_e.TypeAliasType('FlatList', list[T], type_params=(T,)) |
| 120 |
InnerList = t_e.TypeAliasType('InnerList', t.Annotated[FlatList[T], 1], type_params=(T,)) |
| 121 |
MyList = t_e.TypeAliasType('MyList', t.Annotated[InnerList[T], 2], type_params=(T,)) |
| 122 |
MyIntList = t_e.TypeAliasType('MyIntList', MyList[int]) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize( |
| 126 |
'unpack_mode', |
| 127 |
['eager', 'lenient'], |
| 128 |
) |
| 129 |
def test_unpack_type_aliases_generic(unpack_mode: Literal['eager', 'lenient']) -> None: |
| 130 |
result = inspect_annotation( |
| 131 |
MyIntList, |
| 132 |
annotation_source=AnnotationSource.ANY, |
| 133 |
unpack_type_aliases=unpack_mode, |
| 134 |
) |
| 135 |
|
| 136 |
assert result.type == FlatList[int] |
| 137 |
assert result.metadata == [1, 2] |
| 138 |
|
| 139 |
|
| 140 |
@pytest.mark.skipif(sys.version_info < (3, 12), reason='Requires new `type` statement syntax.') |
| 141 |
def test_unpack_type_aliases_undefined_eager_fails(create_module) -> None: |
| 142 |
code = dedent(""" |
| 143 |
from typing import Annotated |
| 144 |
|
| 145 |
type TestType0 = Annotated[Undefined, 1] |
| 146 |
type TestType1 = Annotated[TestType0, 2] |
| 147 |
|
| 148 |
type TestType2[T] = Undefined |
| 149 |
type TestType3 = Annotated[TestType2[int], 2] |
| 150 |
""") |
| 151 |
|
| 152 |
module = create_module(code) |
| 153 |
|
| 154 |
with pytest.raises(NameError) as exc_info: |
| 155 |
inspect_annotation( |
| 156 |
module.TestType1, |
| 157 |
annotation_source=AnnotationSource.ANY, |
| 158 |
unpack_type_aliases='eager', |
| 159 |
) |
| 160 |
|
| 161 |
assert exc_info.value.name == 'Undefined' |
| 162 |
|
| 163 |
with pytest.raises(NameError) as exc_info: |
| 164 |
inspect_annotation( |
| 165 |
module.TestType3, |
| 166 |
annotation_source=AnnotationSource.ANY, |
| 167 |
unpack_type_aliases='eager', |
| 168 |
) |
| 169 |
|
| 170 |
assert exc_info.value.name == 'Undefined' |
| 171 |
|
| 172 |
|
| 173 |
@pytest.mark.skipif(sys.version_info < (3, 12), reason='Requires new `type` statement syntax.') |
| 174 |
def test_unpack_type_aliases_undefined_lenient(create_module) -> None: |
| 175 |
code = dedent(""" |
| 176 |
from typing import Annotated |
| 177 |
|
| 178 |
type TestType0 = Annotated[Undefined, 1] |
| 179 |
type TestType1 = Annotated[TestType0, 2] |
| 180 |
|
| 181 |
type TestType2[T] = Undefined |
| 182 |
type TestType3 = Annotated[TestType2[int], 2] |
| 183 |
""") |
| 184 |
|
| 185 |
module = create_module(code) |
| 186 |
|
| 187 |
result_1 = inspect_annotation( |
| 188 |
t.Annotated[t.Final[module.TestType1], 3], |
| 189 |
annotation_source=AnnotationSource.ANY, |
| 190 |
unpack_type_aliases='lenient', |
| 191 |
) |
| 192 |
|
| 193 |
assert result_1.type is module.TestType0 |
| 194 |
assert result_1.qualifiers == {'final'} |
| 195 |
assert result_1.metadata == [2, 3] |
| 196 |
|
| 197 |
result_2 = inspect_annotation( |
| 198 |
t.Annotated[t.Final[module.TestType3], 3], |
| 199 |
annotation_source=AnnotationSource.ANY, |
| 200 |
unpack_type_aliases='lenient', |
| 201 |
) |
| 202 |
|
| 203 |
assert t_e.get_origin(result_2.type) is module.TestType2 |
| 204 |
assert t_e.get_args(result_2.type)[0] is int |
| 205 |
assert result_2.qualifiers == {'final'} |
| 206 |
assert result_2.metadata == [2, 3] |