Skip to content

Commit b6f2c67

Browse files
committed
port test_deprecate from Trio
1 parent 4476608 commit b6f2c67

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

tests/module_with_deprecations.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
regular = "hi"
2+
3+
from trio_asyncio import _deprecate
4+
5+
_deprecate.enable_attribute_deprecations(__name__)
6+
7+
# Make sure that we don't trigger infinite recursion when accessing module
8+
# attributes in between calling enable_attribute_deprecations and defining
9+
# __deprecated_attributes__:
10+
import sys
11+
12+
this_mod = sys.modules[__name__]
13+
assert this_mod.regular == "hi"
14+
assert not hasattr(this_mod, "dep1")
15+
16+
__deprecated_attributes__ = {
17+
"dep1": _deprecate.DeprecatedAttribute("value1", "1.1", issue=1),
18+
"dep2": _deprecate.DeprecatedAttribute(
19+
"value2", "1.2", issue=1, instead="instead-string"
20+
),
21+
}

tests/test_deprecate.py

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
# Mostly copied from trio.tests.test_deprecate.
2+
3+
import pytest
4+
5+
import inspect
6+
import warnings
7+
8+
from trio_asyncio._deprecate import (
9+
TrioAsyncioDeprecationWarning,
10+
warn_deprecated,
11+
deprecated,
12+
deprecated_alias,
13+
)
14+
15+
from . import module_with_deprecations
16+
17+
18+
@pytest.fixture
19+
def recwarn_always(recwarn):
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():
28+
info = inspect.getframeinfo(inspect.currentframe().f_back)
29+
return (info.filename, info.lineno)
30+
31+
32+
def test_warn_deprecated(recwarn_always):
33+
def deprecated_thing():
34+
warn_deprecated("ice", "1.2", issue=1, instead="water")
35+
36+
deprecated_thing()
37+
filename, lineno = _here()
38+
assert len(recwarn_always) == 1
39+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
40+
assert "ice is deprecated" in got.message.args[0]
41+
assert "trio-asyncio 1.2" in got.message.args[0]
42+
assert "water instead" in got.message.args[0]
43+
assert "/issues/1" in got.message.args[0]
44+
assert got.filename == filename
45+
assert got.lineno == lineno - 1
46+
47+
48+
def test_warn_deprecated_no_instead_or_issue(recwarn_always):
49+
# Explicitly no instead or issue
50+
warn_deprecated("water", "1.3", issue=None, instead=None)
51+
assert len(recwarn_always) == 1
52+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
53+
assert "water is deprecated" in got.message.args[0]
54+
assert "no replacement" in got.message.args[0]
55+
assert "trio-asyncio 1.3" in got.message.args[0]
56+
57+
58+
def test_warn_deprecated_stacklevel(recwarn_always):
59+
def nested1():
60+
nested2()
61+
62+
def nested2():
63+
warn_deprecated("x", "1.3", issue=7, instead="y", stacklevel=3)
64+
65+
filename, lineno = _here()
66+
nested1()
67+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
68+
assert got.filename == filename
69+
assert got.lineno == lineno + 1
70+
71+
72+
def old(): # pragma: no cover
73+
pass
74+
75+
76+
def new(): # pragma: no cover
77+
pass
78+
79+
80+
def test_warn_deprecated_formatting(recwarn_always):
81+
warn_deprecated(old, "1.0", issue=1, instead=new)
82+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
83+
assert "test_deprecate.old is deprecated" in got.message.args[0]
84+
assert "test_deprecate.new instead" in got.message.args[0]
85+
86+
87+
@deprecated("1.5", issue=123, instead=new)
88+
def deprecated_old():
89+
return 3
90+
91+
92+
def test_deprecated_decorator(recwarn_always):
93+
assert deprecated_old() == 3
94+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
95+
assert "test_deprecate.deprecated_old is deprecated" in got.message.args[0]
96+
assert "1.5" in got.message.args[0]
97+
assert "test_deprecate.new" in got.message.args[0]
98+
assert "issues/123" in got.message.args[0]
99+
100+
101+
class Foo:
102+
@deprecated("1.0", issue=123, instead="crying")
103+
def method(self):
104+
return 7
105+
106+
107+
def test_deprecated_decorator_method(recwarn_always):
108+
f = Foo()
109+
assert f.method() == 7
110+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
111+
assert "test_deprecate.Foo.method is deprecated" in got.message.args[0]
112+
113+
114+
@deprecated("1.2", thing="the thing", issue=None, instead=None)
115+
def deprecated_with_thing():
116+
return 72
117+
118+
119+
def test_deprecated_decorator_with_explicit_thing(recwarn_always):
120+
assert deprecated_with_thing() == 72
121+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
122+
assert "the thing is deprecated" in got.message.args[0]
123+
124+
125+
def new_hotness():
126+
return "new hotness"
127+
128+
129+
old_hotness = deprecated_alias("old_hotness", new_hotness, "1.23", issue=1)
130+
131+
132+
def test_deprecated_alias(recwarn_always):
133+
assert old_hotness() == "new hotness"
134+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
135+
assert "test_deprecate.old_hotness is deprecated" in got.message.args[0]
136+
assert "1.23" in got.message.args[0]
137+
assert "test_deprecate.new_hotness instead" in got.message.args[0]
138+
assert "issues/1" in got.message.args[0]
139+
140+
assert ".. deprecated:: 1.23" in old_hotness.__doc__
141+
assert "test_deprecate.new_hotness instead" in old_hotness.__doc__
142+
assert "issues/1>`__" in old_hotness.__doc__
143+
144+
145+
class Alias:
146+
def new_hotness_method(self):
147+
return "new hotness method"
148+
149+
old_hotness_method = deprecated_alias(
150+
"Alias.old_hotness_method", new_hotness_method, "3.21", issue=1
151+
)
152+
153+
154+
def test_deprecated_alias_method(recwarn_always):
155+
obj = Alias()
156+
assert obj.old_hotness_method() == "new hotness method"
157+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
158+
msg = got.message.args[0]
159+
assert "test_deprecate.Alias.old_hotness_method is deprecated" in msg
160+
assert "test_deprecate.Alias.new_hotness_method instead" in msg
161+
162+
163+
@deprecated("2.1", issue=1, instead="hi")
164+
def docstring_test1(): # pragma: no cover
165+
"""Hello!"""
166+
167+
168+
@deprecated("2.1", issue=None, instead="hi")
169+
def docstring_test2(): # pragma: no cover
170+
"""Hello!"""
171+
172+
173+
@deprecated("2.1", issue=1, instead=None)
174+
def docstring_test3(): # pragma: no cover
175+
"""Hello!"""
176+
177+
178+
@deprecated("2.1", issue=None, instead=None)
179+
def docstring_test4(): # pragma: no cover
180+
"""Hello!"""
181+
182+
183+
def test_deprecated_docstring_munging():
184+
assert (
185+
docstring_test1.__doc__
186+
== """Hello!
187+
188+
.. deprecated:: 2.1
189+
Use hi instead.
190+
For details, see `issue #1 <https://github.com/python-trio/trio-asyncio/issues/1>`__.
191+
192+
"""
193+
)
194+
195+
assert (
196+
docstring_test2.__doc__
197+
== """Hello!
198+
199+
.. deprecated:: 2.1
200+
Use hi instead.
201+
202+
"""
203+
)
204+
205+
assert (
206+
docstring_test3.__doc__
207+
== """Hello!
208+
209+
.. deprecated:: 2.1
210+
For details, see `issue #1 <https://github.com/python-trio/trio-asyncio/issues/1>`__.
211+
212+
"""
213+
)
214+
215+
assert (
216+
docstring_test4.__doc__
217+
== """Hello!
218+
219+
.. deprecated:: 2.1
220+
221+
"""
222+
)
223+
224+
225+
def test_module_with_deprecations(recwarn_always):
226+
assert module_with_deprecations.regular == "hi"
227+
assert len(recwarn_always) == 0
228+
229+
filename, lineno = _here()
230+
assert module_with_deprecations.dep1 == "value1"
231+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
232+
assert got.filename == filename
233+
assert got.lineno == lineno + 1
234+
235+
assert "module_with_deprecations.dep1" in got.message.args[0]
236+
assert "trio-asyncio 1.1" in got.message.args[0]
237+
assert "/issues/1" in got.message.args[0]
238+
assert "value1 instead" in got.message.args[0]
239+
240+
assert module_with_deprecations.dep2 == "value2"
241+
got = recwarn_always.pop(TrioAsyncioDeprecationWarning)
242+
assert "instead-string instead" in got.message.args[0]
243+
244+
with pytest.raises(AttributeError):
245+
module_with_deprecations.asdf

0 commit comments

Comments
 (0)