-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_utils_deprecation.py
130 lines (101 loc) · 4.55 KB
/
test_utils_deprecation.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
import unittest
import warnings
import pytest
from huggingface_hub.utils._deprecation import (
_deprecate_arguments,
_deprecate_method,
_deprecate_positional_args,
)
class TestDeprecationUtils(unittest.TestCase):
def test_deprecate_positional_args(self):
"""Test warnings are triggered when using deprecated positional args."""
@_deprecate_positional_args(version="xxx")
def dummy_position_deprecated(a, *, b="b", c="c"):
pass
with warnings.catch_warnings():
# Assert no warnings when used correctly.
# Taken from https://docs.pytest.org/en/latest/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests
warnings.simplefilter("error")
dummy_position_deprecated(a="A", b="B", c="C")
dummy_position_deprecated("A", b="B", c="C")
with pytest.warns(FutureWarning):
dummy_position_deprecated("A", "B", c="C")
with pytest.warns(FutureWarning):
dummy_position_deprecated("A", "B", "C")
def test_deprecate_arguments(self):
"""Test warnings are triggered when using deprecated arguments."""
@_deprecate_arguments(version="xxx", deprecated_args={"c"})
def dummy_c_deprecated(a, b="b", c="c"):
pass
@_deprecate_arguments(version="xxx", deprecated_args={"b", "c"})
def dummy_b_c_deprecated(a, b="b", c="c"):
pass
with warnings.catch_warnings():
# Assert no warnings when used correctly.
# Taken from https://docs.pytest.org/en/latest/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests
warnings.simplefilter("error")
dummy_c_deprecated("A")
dummy_c_deprecated("A", "B")
dummy_c_deprecated("A", b="B")
dummy_b_c_deprecated("A")
dummy_b_c_deprecated("A", b="b")
dummy_b_c_deprecated("A", b="b", c="c")
with pytest.warns(FutureWarning):
dummy_c_deprecated("A", "B", "C")
with pytest.warns(FutureWarning):
dummy_c_deprecated("A", c="C")
with pytest.warns(FutureWarning):
dummy_c_deprecated("A", b="B", c="C")
with pytest.warns(FutureWarning):
dummy_b_c_deprecated("A", b="B")
with pytest.warns(FutureWarning):
dummy_b_c_deprecated("A", c="C")
with pytest.warns(FutureWarning):
dummy_b_c_deprecated("A", b="B", c="C")
def test_deprecate_arguments_with_default_warning_message(self) -> None:
"""Test default warning message when deprecating arguments."""
@_deprecate_arguments(version="xxx", deprecated_args={"a"})
def dummy_deprecated_default_message(a: str = "a") -> None:
pass
# Default message
with pytest.warns(FutureWarning) as record:
dummy_deprecated_default_message(a="A")
self.assertEqual(len(record), 1)
self.assertEqual(
record[0].message.args[0],
"Deprecated argument(s) used in 'dummy_deprecated_default_message': a."
" Will not be supported from version 'xxx'.",
)
def test_deprecate_arguments_with_custom_warning_message(self) -> None:
"""Test custom warning message when deprecating arguments."""
@_deprecate_arguments(
version="xxx",
deprecated_args={"a"},
custom_message="This is a custom message.",
)
def dummy_deprecated_custom_message(a: str = "a") -> None:
pass
# Custom message
with pytest.warns(FutureWarning) as record:
dummy_deprecated_custom_message(a="A")
self.assertEqual(len(record), 1)
self.assertEqual(
record[0].message.args[0],
"Deprecated argument(s) used in 'dummy_deprecated_custom_message': a."
" Will not be supported from version 'xxx'.\n\nThis is a custom"
" message.",
)
def test_deprecated_method(self) -> None:
"""Test deprecate method throw warning."""
@_deprecate_method(version="xxx", message="This is a custom message.")
def dummy_deprecated() -> None:
pass
# Custom message
with pytest.warns(FutureWarning) as record:
dummy_deprecated()
self.assertEqual(len(record), 1)
self.assertEqual(
record[0].message.args[0],
"'dummy_deprecated' (from 'tests.test_utils_deprecation') is deprecated"
" and will be removed from version 'xxx'. This is a custom message.",
)