Debian Codegraph @

src/trio/_tests/test_deprecate.py

raw
1 from __future__ import annotations
2
3 import inspect
4 import warnings
5 from types import ModuleType
6
7 import pytest
8
9 from .._deprecate import (
10 TrioDeprecationWarning,
11 deprecated,
12 deprecated_alias,
13 warn_deprecated,
14 )
15 from . import module_with_deprecations
16
17
18 @pytest.fixture
19 def recwarn_always(recwarn: pytest.WarningsRecorder) -> pytest.WarningsRecorder:
20 warnings.simplefilter("always")
21 # ResourceWarnings about unclosed sockets can occur nondeterministically
22 # (during GC) which throws off the tests in this file
23 warnings.simplefilter("ignore", ResourceWarning)
24 return recwarn
25
26
27 def _here() -> tuple[str, int]:
28 frame = inspect.currentframe()
29 assert frame is not None
30 assert frame.f_back is not None
31 info = inspect.getframeinfo(frame.f_back)
32 return (info.filename, info.lineno)
33
34
35 def test_warn_deprecated(recwarn_always: pytest.WarningsRecorder) -> None:
36 def deprecated_thing() -> None:
37 warn_deprecated("ice", "1.2", issue=1, instead="water")
38
39 deprecated_thing()
40 filename, lineno = _here()
41 assert len(recwarn_always) == 1
42 got = recwarn_always.pop(DeprecationWarning)
43 assert isinstance(got.message, Warning)
44 assert "ice is deprecated" in got.message.args[0]
45 assert "Trio 1.2" in got.message.args[0]
46 assert "water instead" in got.message.args[0]
47 assert "/issues/1" in got.message.args[0]
48 assert got.filename == filename
49 assert got.lineno == lineno - 1
50
51
52 def test_warn_deprecated_no_instead_or_issue(
53 recwarn_always: pytest.WarningsRecorder,
54 ) -> None:
55 # Explicitly no instead or issue
56 warn_deprecated("water", "1.3", issue=None, instead=None)
57 assert len(recwarn_always) == 1
58 got = recwarn_always.pop(DeprecationWarning)
59 assert isinstance(got.message, Warning)
60 assert "water is deprecated" in got.message.args[0]
61 assert "no replacement" in got.message.args[0]
62 assert "Trio 1.3" in got.message.args[0]
63
64
65 def test_warn_deprecated_stacklevel(recwarn_always: pytest.WarningsRecorder) -> None:
66 def nested1() -> None:
67 nested2()
68
69 def nested2() -> None:
70 warn_deprecated("x", "1.3", issue=7, instead="y", stacklevel=3)
71
72 filename, lineno = _here()
73 nested1()
74 got = recwarn_always.pop(DeprecationWarning)
75 assert got.filename == filename
76 assert got.lineno == lineno + 1
77
78
79 def old() -> None: # pragma: no cover
80 pass
81
82
83 def new() -> None: # pragma: no cover
84 pass
85
86
87 def test_warn_deprecated_formatting(recwarn_always: pytest.WarningsRecorder) -> None:
88 warn_deprecated(old, "1.0", issue=1, instead=new)
89 got = recwarn_always.pop(DeprecationWarning)
90 assert isinstance(got.message, Warning)
91 assert "test_deprecate.old is deprecated" in got.message.args[0]
92 assert "test_deprecate.new instead" in got.message.args[0]
93
94
95 @deprecated("1.5", issue=123, instead=new)
96 def deprecated_old() -> int:
97 return 3
98
99
100 def test_deprecated_decorator(recwarn_always: pytest.WarningsRecorder) -> None:
101 assert deprecated_old() == 3
102 got = recwarn_always.pop(DeprecationWarning)
103 assert isinstance(got.message, Warning)
104 assert "test_deprecate.deprecated_old is deprecated" in got.message.args[0]
105 assert "1.5" in got.message.args[0]
106 assert "test_deprecate.new" in got.message.args[0]
107 assert "issues/123" in got.message.args[0]
108
109
110 class Foo:
111 @deprecated("1.0", issue=123, instead="crying")
112 def method(self) -> int:
113 return 7
114
115
116 def test_deprecated_decorator_method(recwarn_always: pytest.WarningsRecorder) -> None:
117 f = Foo()
118 assert f.method() == 7
119 got = recwarn_always.pop(DeprecationWarning)
120 assert isinstance(got.message, Warning)
121 assert "test_deprecate.Foo.method is deprecated" in got.message.args[0]
122
123
124 @deprecated("1.2", thing="the thing", issue=None, instead=None)
125 def deprecated_with_thing() -> int:
126 return 72
127
128
129 def test_deprecated_decorator_with_explicit_thing(
130 recwarn_always: pytest.WarningsRecorder,
131 ) -> None:
132 assert deprecated_with_thing() == 72
133 got = recwarn_always.pop(DeprecationWarning)
134 assert isinstance(got.message, Warning)
135 assert "the thing is deprecated" in got.message.args[0]
136
137
138 def new_hotness() -> str:
139 return "new hotness"
140
141
142 old_hotness = deprecated_alias("old_hotness", new_hotness, "1.23", issue=1)
143
144
145 def test_deprecated_alias(recwarn_always: pytest.WarningsRecorder) -> None:
146 assert old_hotness() == "new hotness"
147 got = recwarn_always.pop(DeprecationWarning)
148 assert isinstance(got.message, Warning)
149 assert "test_deprecate.old_hotness is deprecated" in got.message.args[0]
150 assert "1.23" in got.message.args[0]
151 assert "test_deprecate.new_hotness instead" in got.message.args[0]
152 assert "issues/1" in got.message.args[0]
153
154 assert isinstance(old_hotness.__doc__, str)
155 assert ".. deprecated:: 1.23" in old_hotness.__doc__
156 assert "test_deprecate.new_hotness instead" in old_hotness.__doc__
157 assert "issues/1>`__" in old_hotness.__doc__
158
159
160 class Alias:
161 def new_hotness_method(self) -> str:
162 return "new hotness method"
163
164 old_hotness_method = deprecated_alias(
165 "Alias.old_hotness_method",
166 new_hotness_method,
167 "3.21",
168 issue=1,
169 )
170
171
172 def test_deprecated_alias_method(recwarn_always: pytest.WarningsRecorder) -> None:
173 obj = Alias()
174 assert obj.old_hotness_method() == "new hotness method"
175 got = recwarn_always.pop(DeprecationWarning)
176 assert isinstance(got.message, Warning)
177 msg = got.message.args[0]
178 assert "test_deprecate.Alias.old_hotness_method is deprecated" in msg
179 assert "test_deprecate.Alias.new_hotness_method instead" in msg
180
181
182 @deprecated("2.1", issue=1, instead="hi")
183 def docstring_test1() -> None: # pragma: no cover
184 """Hello!"""
185
186
187 @deprecated("2.1", issue=None, instead="hi")
188 def docstring_test2() -> None: # pragma: no cover
189 """Hello!"""
190
191
192 @deprecated("2.1", issue=1, instead=None)
193 def docstring_test3() -> None: # pragma: no cover
194 """Hello!"""
195
196
197 @deprecated("2.1", issue=None, instead=None)
198 def docstring_test4() -> None: # pragma: no cover
199 """Hello!"""
200
201
202 def test_deprecated_docstring_munging() -> None:
203 assert docstring_test1.__doc__ == """Hello!
204
205 .. deprecated:: 2.1
206 Use hi instead.
207 For details, see `issue #1 <https://github.com/python-trio/trio/issues/1>`__.
208
209 """
210
211 assert docstring_test2.__doc__ == """Hello!
212
213 .. deprecated:: 2.1
214 Use hi instead.
215
216 """
217
218 assert docstring_test3.__doc__ == """Hello!
219
220 .. deprecated:: 2.1
221 For details, see `issue #1 <https://github.com/python-trio/trio/issues/1>`__.
222
223 """
224
225 assert docstring_test4.__doc__ == """Hello!
226
227 .. deprecated:: 2.1
228
229 """
230
231
232 def test_module_with_deprecations(recwarn_always: pytest.WarningsRecorder) -> None:
233 assert module_with_deprecations.regular == "hi"
234 assert len(recwarn_always) == 0
235
236 assert type(module_with_deprecations) is ModuleType
237
238 filename, lineno = _here()
239 assert module_with_deprecations.dep1 == "value1" # type: ignore[attr-defined]
240 got = recwarn_always.pop(DeprecationWarning)
241 assert isinstance(got.message, Warning)
242 assert got.filename == filename
243 assert got.lineno == lineno + 1
244
245 assert "module_with_deprecations.dep1" in got.message.args[0]
246 assert "Trio 1.1" in got.message.args[0]
247 assert "/issues/1" in got.message.args[0]
248 assert "value1 instead" in got.message.args[0]
249
250 assert module_with_deprecations.dep2 == "value2" # type: ignore[attr-defined]
251 got = recwarn_always.pop(DeprecationWarning)
252 assert isinstance(got.message, Warning)
253 assert "instead-string instead" in got.message.args[0]
254
255 with pytest.raises(AttributeError):
256 module_with_deprecations.asdf # type: ignore[attr-defined] # noqa: B018 # "useless expression"
257
258
259 def test_warning_class() -> None:
260 with pytest.deprecated_call():
261 warn_deprecated("foo", "bar", issue=None, instead=None)
262
263 # essentially the same as the above check
264 with pytest.warns(
265 DeprecationWarning,
266 match="^foo is deprecated since Trio bar with no replacement$",
267 ):
268 warn_deprecated("foo", "bar", issue=None, instead=None)
269
270 with pytest.warns(TrioDeprecationWarning):
271 warn_deprecated(
272 "foo",
273 "bar",
274 issue=None,
275 instead=None,
276 use_triodeprecationwarning=True,
277 )