Skip to content

Commit 8b31067

Browse files
committed
Deprecate pytest.inlineCallbacks and pytest.blockon, use pytest_twisted.*
1 parent 681fa3d commit 8b31067

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

pytest_twisted.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import functools
12
import inspect
3+
import warnings
24

35
import decorator
46
import greenlet
@@ -22,6 +24,25 @@ class _instances:
2224
reactor = None
2325

2426

27+
def _deprecate(deprecated, recommended):
28+
def decorator(f):
29+
@functools.wraps(f)
30+
def wrapper(*args, **kwargs):
31+
warnings.warn(
32+
'{deprecated} has been deprecated, use {recommended}'.format(
33+
deprecated=deprecated,
34+
recommended=recommended,
35+
),
36+
DeprecationWarning,
37+
stacklevel=2,
38+
)
39+
return f(*args, **kwargs)
40+
41+
return wrapper
42+
43+
return decorator
44+
45+
2546
def blockon(d):
2647
if _config.external_reactor:
2748
return block_from_thread(d)
@@ -185,6 +206,13 @@ def pytest_addoption(parser):
185206

186207

187208
def pytest_configure(config):
188-
pytest.inlineCallbacks = inlineCallbacks
189-
pytest.blockon = blockon
209+
pytest.inlineCallbacks = _deprecate(
210+
deprecated='pytest.inlineCallbacks',
211+
recommended='pytest_twisted.inlineCallbacks',
212+
)(inlineCallbacks)
213+
pytest.blockon = _deprecate(
214+
deprecated='pytest.blockon',
215+
recommended='pytest_twisted.blockon',
216+
)(blockon)
217+
190218
reactor_installers[config.getoption("reactor")]()

testing/test_basic.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,73 @@ def test_inline_callbacks_in_pytest():
5757
assert hasattr(pytest, 'inlineCallbacks')
5858

5959

60+
def test_inline_callbacks_in_pytest_deprecation(testdir, cmd_opts):
61+
test_file = """
62+
import warnings
63+
64+
from twisted.internet import reactor, defer
65+
import pytest
66+
import pytest_twisted
67+
68+
warnings.simplefilter("always")
69+
70+
@pytest.mark.parametrize(
71+
argnames='decorator, warning_count',
72+
argvalues=[
73+
[pytest.inlineCallbacks, 1],
74+
[pytest_twisted.inlineCallbacks, 0],
75+
],
76+
)
77+
def test_inline_callbacks_in_pytest_deprecated(decorator, warning_count):
78+
with warnings.catch_warnings(record=True) as w:
79+
@decorator
80+
def f():
81+
yield 42
82+
83+
assert len(w) == warning_count
84+
"""
85+
testdir.makepyfile(test_file)
86+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
87+
assert_outcomes(rr, {"passed": 2})
88+
89+
6090
def test_blockon_in_pytest():
6191
assert hasattr(pytest, 'blockon')
6292

6393

94+
def test_blockon_in_pytest_deprecation(testdir, cmd_opts):
95+
test_file = """
96+
import warnings
97+
98+
from twisted.internet import reactor, defer
99+
import pytest
100+
import pytest_twisted
101+
102+
warnings.simplefilter("always")
103+
104+
@pytest.fixture(
105+
scope="module",
106+
params=[
107+
[pytest.blockon, 1],
108+
[pytest_twisted.blockon, 0],
109+
],
110+
)
111+
def foo(request):
112+
d = defer.Deferred()
113+
d.callback(None)
114+
with warnings.catch_warnings(record=True) as w:
115+
request.param[0](d)
116+
117+
return (w, request.param[1])
118+
119+
def test_succeed(foo):
120+
assert len(foo[0]) == foo[1]
121+
"""
122+
testdir.makepyfile(test_file)
123+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
124+
assert_outcomes(rr, {"passed": 2})
125+
126+
64127
def test_fail_later(testdir, cmd_opts):
65128
test_file = """
66129
from twisted.internet import reactor, defer

0 commit comments

Comments
 (0)