forked from optuna/optuna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_deprecated.py
203 lines (150 loc) · 6.54 KB
/
test_deprecated.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from typing import Any
from typing import Optional
import pytest
from optuna import _deprecated
class _Sample(object):
def __init__(self, a: Any, b: Any, c: Any) -> None:
pass
def _method(self) -> None:
"""summary
detail
"""
pass
def _method_expected(self) -> None:
"""summary
detail
.. warning::
Deprecated in v1.1.0. This feature will be removed in the future. The removal of this
feature is currently scheduled for v3.0.0, but this schedule is subject to change.
See https://github.com/optuna/optuna/releases/tag/v1.1.0.
"""
pass
@pytest.mark.parametrize("deprecated_version", ["1.1", 100, None, "2.0.0"])
@pytest.mark.parametrize("removed_version", ["1.1", 10, "1.0.0"])
def test_deprecation_raises_error_for_invalid_version(
deprecated_version: Any, removed_version: Any
) -> None:
with pytest.raises(ValueError):
_deprecated.deprecated(deprecated_version, removed_version)
def test_deprecation_decorator() -> None:
deprecated_version = "1.1.0"
removed_version = "3.0.0"
decorator_deprecation = _deprecated.deprecated(deprecated_version, removed_version)
assert callable(decorator_deprecation)
def _func() -> int:
return 10
decorated_func = decorator_deprecation(_func)
assert decorated_func.__name__ == _func.__name__
assert decorated_func.__doc__ == _deprecated._DEPRECATION_NOTE_TEMPLATE.format(
d_ver=deprecated_version, r_ver=removed_version
)
with pytest.warns(FutureWarning):
decorated_func()
def test_deprecation_method_decorator() -> None:
deprecated_version = "1.1.0"
removed_version = "3.0.0"
decorator_deprecation = _deprecated.deprecated(deprecated_version, removed_version)
assert callable(decorator_deprecation)
decorated_method = decorator_deprecation(_Sample._method)
assert decorated_method.__name__ == _Sample._method.__name__
assert decorated_method.__doc__ == _Sample._method_expected.__doc__
with pytest.warns(FutureWarning):
decorated_method(None)
def test_deprecation_class_decorator() -> None:
deprecated_version = "1.1.0"
removed_version = "3.0.0"
decorator_deprecation = _deprecated.deprecated(deprecated_version, removed_version)
assert callable(decorator_deprecation)
decorated_class = decorator_deprecation(_Sample)
assert decorated_class.__name__ == "_Sample"
assert decorated_class.__init__.__name__ == "__init__"
assert decorated_class.__doc__ == _deprecated._DEPRECATION_NOTE_TEMPLATE.format(
d_ver=deprecated_version, r_ver=removed_version
)
with pytest.warns(FutureWarning):
decorated_class("a", "b", "c")
def test_deprecation_class_decorator_name() -> None:
name = "foo"
decorator_deprecation = _deprecated.deprecated("1.1.0", "3.0.0", name=name)
decorated_sample = decorator_deprecation(_Sample)
with pytest.warns(FutureWarning) as record:
decorated_sample("a", "b", "c")
assert isinstance(record.list[0].message, Warning)
assert name in record.list[0].message.args[0]
def test_deprecation_decorator_name() -> None:
def _func() -> int:
return 10
name = "bar"
decorator_deprecation = _deprecated.deprecated("1.1.0", "3.0.0", name=name)
decorated_sample_func = decorator_deprecation(_func)
with pytest.warns(FutureWarning) as record:
decorated_sample_func()
assert isinstance(record.list[0].message, Warning)
assert name in record.list[0].message.args[0]
@pytest.mark.parametrize("text", [None, "", "test", "test" * 100])
def test_deprecation_text_specified(text: Optional[str]) -> None:
def _func() -> int:
return 10
decorator_deprecation = _deprecated.deprecated("1.1.0", "3.0.0", text=text)
decorated_func = decorator_deprecation(_func)
expected_func_doc = _deprecated._DEPRECATION_NOTE_TEMPLATE.format(d_ver="1.1.0", r_ver="3.0.0")
if text is None:
pass
elif len(text) > 0:
expected_func_doc += "\n\n " + text + "\n"
else:
expected_func_doc += "\n\n\n"
assert decorated_func.__name__ == _func.__name__
assert decorated_func.__doc__ == expected_func_doc
with pytest.warns(FutureWarning) as record:
decorated_func()
assert isinstance(record.list[0].message, Warning)
expected_warning_message = _deprecated._DEPRECATION_WARNING_TEMPLATE.format(
name="_func", d_ver="1.1.0", r_ver="3.0.0"
)
if text is not None:
expected_warning_message += " " + text
assert record.list[0].message.args[0] == expected_warning_message
@pytest.mark.parametrize("text", [None, "", "test", "test" * 100])
def test_deprecation_class_text_specified(text: Optional[str]) -> None:
class _Class(object):
def __init__(self, a: Any, b: Any, c: Any) -> None:
pass
decorator_deprecation = _deprecated.deprecated("1.1.0", "3.0.0", text=text)
decorated_class = decorator_deprecation(_Class)
expected_class_doc = _deprecated._DEPRECATION_NOTE_TEMPLATE.format(
d_ver="1.1.0", r_ver="3.0.0"
)
if text is None:
pass
elif len(text) > 0:
expected_class_doc += "\n\n " + text + "\n"
else:
expected_class_doc += "\n\n\n"
assert decorated_class.__name__ == _Class.__name__
assert decorated_class.__doc__ == expected_class_doc
with pytest.warns(FutureWarning) as record:
decorated_class(None, None, None)
assert isinstance(record.list[0].message, Warning)
expected_warning_message = _deprecated._DEPRECATION_WARNING_TEMPLATE.format(
name="_Class", d_ver="1.1.0", r_ver="3.0.0"
)
if text is not None:
expected_warning_message += " " + text
assert record.list[0].message.args[0] == expected_warning_message
def test_deprecation_decorator_default_removed_version() -> None:
deprecated_version = "1.1.0"
decorator_deprecation = _deprecated.deprecated(deprecated_version)
assert callable(decorator_deprecation)
def _func() -> int:
return 10
decorated_func = decorator_deprecation(_func)
assert decorated_func.__name__ == _func.__name__
assert decorated_func.__doc__ == _deprecated._DEPRECATION_NOTE_TEMPLATE.format(
d_ver=deprecated_version, r_ver="3.0.0"
)
with pytest.warns(FutureWarning):
decorated_func()
def test_get_removed_version_from_deprecated_version() -> None:
assert _deprecated._get_removed_version_from_deprecated_version("1.0.0") == "3.0.0"
assert _deprecated._get_removed_version_from_deprecated_version("1.5.0") == "3.0.0"