-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_utils_typing.py
129 lines (107 loc) · 3.34 KB
/
test_utils_typing.py
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
import json
import sys
from typing import Optional, Type, Union
import pytest
from huggingface_hub.utils._typing import is_jsonable, is_simple_optional_type, unwrap_simple_optional_type
class NotSerializableClass:
pass
class CustomType:
pass
OBJ_WITH_CIRCULAR_REF = {"hello": "world"}
OBJ_WITH_CIRCULAR_REF["recursive"] = OBJ_WITH_CIRCULAR_REF
@pytest.mark.parametrize(
"data",
[
123, #
3.14,
"Hello, world!",
True,
None,
[],
[1, 2, 3],
[(1, 2.0, "string"), True],
{},
{"name": "Alice", "age": 30},
{0: "LABEL_0", 1.0: "LABEL_1"},
],
)
def test_is_jsonable_success(data):
assert is_jsonable(data)
json.dumps(data)
@pytest.mark.parametrize(
"data",
[
set([1, 2, 3]),
lambda x: x + 1,
NotSerializableClass(),
{"obj": NotSerializableClass()},
OBJ_WITH_CIRCULAR_REF,
],
)
def test_is_jsonable_failure(data):
assert not is_jsonable(data)
with pytest.raises((TypeError, ValueError)):
json.dumps(data)
@pytest.mark.parametrize(
"type_, is_optional",
[
(Optional[int], True),
(Union[None, int], True),
(Union[int, None], True),
(Optional[CustomType], True),
(Union[None, CustomType], True),
(Union[CustomType, None], True),
(int, False),
(None, False),
(Union[int, float, None], False),
(Union[Union[int, float], None], False),
(Optional[Union[int, float]], False),
],
)
def test_is_simple_optional_type(type_: Type, is_optional: bool):
assert is_simple_optional_type(type_) is is_optional
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
@pytest.mark.parametrize(
"type_, is_optional",
[
("int | None", True),
("None | int", True),
("CustomType | None", True),
("None | CustomType", True),
("int | float", False),
("int | float | None", False),
("(int | float) | None", False),
("Union[int, float] | None", False),
],
)
def test_is_simple_optional_type_pipe(type_: str, is_optional: bool):
assert is_simple_optional_type(eval(type_)) is is_optional
@pytest.mark.parametrize(
"optional_type, inner_type",
[
(Optional[int], int),
(Union[int, None], int),
(Union[None, int], int),
(Optional[CustomType], CustomType),
(Union[CustomType, None], CustomType),
(Union[None, CustomType], CustomType),
],
)
def test_unwrap_simple_optional_type(optional_type: Type, inner_type: Type):
assert unwrap_simple_optional_type(optional_type) is inner_type
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
@pytest.mark.parametrize(
"optional_type, inner_type",
[
("None | int", int),
("int | None", int),
("None | CustomType", CustomType),
("CustomType | None", CustomType),
],
)
def test_unwrap_simple_optional_type_pipe(optional_type: str, inner_type: Type):
assert unwrap_simple_optional_type(eval(optional_type)) is inner_type
@pytest.mark.parametrize("non_optional_type", [int, None, CustomType])
def test_unwrap_simple_optional_type_fail(non_optional_type: Type):
with pytest.raises(ValueError):
unwrap_simple_optional_type(non_optional_type)