Skip to content

Commit 62cc037

Browse files
committed
use pendingdeprecationwarning instead. Narrow some filterwarnings. Fix more tests
1 parent dd3472e commit 62cc037

File tree

7 files changed

+48
-29
lines changed

7 files changed

+48
-29
lines changed

src/_pytest/deprecated.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from warnings import warn
1717

1818
from _pytest.warning_types import PytestDeprecationWarning
19+
from _pytest.warning_types import PytestPendingDeprecationWarning
1920
from _pytest.warning_types import PytestRemovedIn9Warning
2021
from _pytest.warning_types import UnformattedWarning
2122

@@ -35,18 +36,24 @@ def decorator(func: object) -> object:
3536
return decorator
3637

3738

38-
CALLABLE_RAISES = PytestDeprecationWarning(
39-
"The callable form of pytest.raises is deprecated.\n"
40-
"Use `with pytest.raises(...):` instead."
39+
CALLABLE_RAISES = PytestPendingDeprecationWarning(
40+
"The callable form of pytest.raises will be deprecated in a future version.\n"
41+
"Use `with pytest.raises(...):` instead.\n"
42+
"Full deprecation will not be made until there's a tool to automatically update"
43+
" code to use the context-manager form"
4144
)
4245

43-
CALLABLE_WARNS = PytestDeprecationWarning(
44-
"The callable form of pytest.warns is deprecated.\n"
46+
CALLABLE_WARNS = PytestPendingDeprecationWarning(
47+
"The callable form of pytest.warns will be deprecated in a future version.\n"
4548
"Use `with pytest.warns(...):` instead."
49+
"Full deprecation will not be made until there's a tool to automatically update"
50+
" code to use the context-manager form"
4651
)
47-
CALLABLE_DEPRECATED_CALL = PytestDeprecationWarning(
48-
"The callable form of pytest.deprecated_call is deprecated.\n"
52+
CALLABLE_DEPRECATED_CALL = PytestPendingDeprecationWarning(
53+
"The callable form of pytest.deprecated_call will be deprecated in a future version.\n"
4954
"Use `with pytest.deprecated_call():` instead."
55+
"Full deprecation will not be made until there's a tool to automatically update"
56+
" code to use the context-manager form"
5057
)
5158

5259

src/_pytest/python_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def raises(
957957
>>> raises(ZeroDivisionError, f, x=0)
958958
<ExceptionInfo ...>
959959
960-
The form above is fully supported but discouraged for new code because the
960+
The form above is going to be deprecated in a future pytest release as the
961961
context manager form is regarded as more readable and less error-prone.
962962
963963
.. note::

src/_pytest/warning_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class PytestCollectionWarning(PytestWarning):
4444
__module__ = "pytest"
4545

4646

47+
class PytestPendingDeprecationWarning(PytestWarning, PendingDeprecationWarning):
48+
"""Warning emitted for features that will be deprecated in a future version."""
49+
50+
__module__ = "pytest"
51+
52+
4753
class PytestDeprecationWarning(PytestWarning, DeprecationWarning):
4854
"""Warning class for features that will be removed in a future version."""
4955

src/pytest/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
from _pytest.warning_types import PytestDeprecationWarning
8282
from _pytest.warning_types import PytestExperimentalApiWarning
8383
from _pytest.warning_types import PytestFDWarning
84+
from _pytest.warning_types import PytestPendingDeprecationWarning
8485
from _pytest.warning_types import PytestRemovedIn9Warning
8586
from _pytest.warning_types import PytestUnhandledThreadExceptionWarning
8687
from _pytest.warning_types import PytestUnknownMarkWarning
@@ -130,6 +131,7 @@
130131
"PytestDeprecationWarning",
131132
"PytestExperimentalApiWarning",
132133
"PytestFDWarning",
134+
"PytestPendingDeprecationWarning",
133135
"PytestPluginManager",
134136
"PytestRemovedIn9Warning",
135137
"PytestUnhandledThreadExceptionWarning",

testing/python/raises.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ def __call__(self):
225225
refcount = len(gc.get_referrers(t))
226226

227227
if method == "function":
228-
with pytest.warns(pytest.PytestDeprecationWarning):
228+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
229229
pytest.raises(ValueError, t)
230230
elif method == "function_match":
231-
with pytest.warns(pytest.PytestDeprecationWarning):
231+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
232232
pytest.raises(ValueError, t).match("^$")
233233
elif method == "with":
234234
with pytest.raises(ValueError):
@@ -267,22 +267,22 @@ def test_raises_match(self) -> None:
267267
int("asdf", base=10)
268268

269269
# "match" without context manager.
270-
with pytest.warns(pytest.PytestDeprecationWarning):
270+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
271271
pytest.raises(ValueError, int, "asdf").match("invalid literal")
272272
with pytest.raises(AssertionError) as excinfo:
273-
with pytest.warns(pytest.PytestDeprecationWarning):
273+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
274274
pytest.raises(ValueError, int, "asdf").match(msg)
275275
assert str(excinfo.value) == expr
276276

277-
with pytest.warns(pytest.PytestDeprecationWarning):
277+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
278278
pytest.raises(TypeError, int, match="invalid") # type: ignore[call-overload]
279279

280280
def tfunc(match):
281281
raise ValueError(f"match={match}")
282282

283-
with pytest.warns(pytest.PytestDeprecationWarning):
283+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
284284
pytest.raises(ValueError, tfunc, match="asdf").match("match=asdf")
285-
with pytest.warns(pytest.PytestDeprecationWarning):
285+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
286286
pytest.raises(ValueError, tfunc, match="").match("match=")
287287

288288
# empty string matches everything, which is probably not what the user wants
@@ -411,5 +411,5 @@ def test_callable_func_kwarg(self) -> None:
411411
def my_raise() -> None:
412412
raise ValueError
413413

414-
with pytest.warns(pytest.PytestDeprecationWarning):
414+
with pytest.warns(pytest.PytestPendingDeprecationWarning):
415415
pytest.raises(expected_exception=ValueError, func=my_raise)

testing/test_cacheprovider.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def test_config_cache_dataerror(self, pytester: Pytester) -> None:
5151
config = pytester.parseconfigure()
5252
assert config.cache is not None
5353
cache = config.cache
54-
pytest.raises(TypeError, lambda: cache.set("key/name", cache))
54+
with pytest.raises(TypeError):
55+
cache.set("key/name", cache)
5556
config.cache.set("key/name", 0)
5657
config.cache._getvaluepath("key/name").write_bytes(b"123invalid")
5758
val = config.cache.get("key/name", -2)
@@ -143,7 +144,8 @@ def test_cachefuncarg(cache):
143144
val = cache.get("some/thing", None)
144145
assert val is None
145146
cache.set("some/thing", [1])
146-
pytest.raises(TypeError, lambda: cache.get("some/thing"))
147+
with pytest.raises(TypeError):
148+
cache.get("some/thing")
147149
val = cache.get("some/thing", [])
148150
assert val == [1]
149151
"""

testing/test_recwarn.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import warnings
77

8-
from _pytest.warning_types import PytestDeprecationWarning
8+
from _pytest.warning_types import PytestPendingDeprecationWarning
99
import pytest
1010
from pytest import ExitCode
1111
from pytest import Pytester
@@ -161,7 +161,7 @@ def test_deprecated_call(self) -> None:
161161

162162
def test_deprecated_call_ret(self) -> None:
163163
with pytest.warns(
164-
PytestDeprecationWarning,
164+
PytestPendingDeprecationWarning,
165165
match=(
166166
wrap_escape(
167167
"The callable form of pytest.deprecated_call is deprecated.\n"
@@ -212,7 +212,7 @@ def f():
212212
msg = "No warnings of type (.*DeprecationWarning.*, .*PendingDeprecationWarning.*)"
213213
with pytest.raises(pytest.fail.Exception, match=msg):
214214
if mode == "call":
215-
with pytest.warns(PytestDeprecationWarning):
215+
with pytest.warns(PytestPendingDeprecationWarning):
216216
pytest.deprecated_call(f)
217217
else:
218218
with pytest.deprecated_call():
@@ -223,7 +223,7 @@ def f():
223223
)
224224
@pytest.mark.parametrize("mode", ["context_manager", "call"])
225225
@pytest.mark.parametrize("call_f_first", [True, False])
226-
@pytest.mark.filterwarnings("ignore")
226+
@pytest.mark.filterwarnings("ignore:hi")
227227
def test_deprecated_call_modes(self, warning_type, mode, call_f_first) -> None:
228228
"""Ensure deprecated_call() captures a deprecation warning as expected inside its
229229
block/function.
@@ -237,7 +237,8 @@ def f():
237237
if call_f_first:
238238
assert f() == 10
239239
if mode == "call":
240-
assert pytest.deprecated_call(f) == 10
240+
with pytest.warns(PytestPendingDeprecationWarning):
241+
assert pytest.deprecated_call(f) == 10
241242
else:
242243
with pytest.deprecated_call():
243244
assert f() == 10
@@ -258,7 +259,7 @@ def f():
258259

259260
with pytest.warns(warning):
260261
with pytest.raises(pytest.fail.Exception):
261-
with pytest.warns(PytestDeprecationWarning):
262+
with pytest.warns(PytestPendingDeprecationWarning):
262263
pytest.deprecated_call(f)
263264
with pytest.raises(pytest.fail.Exception):
264265
with pytest.deprecated_call():
@@ -293,7 +294,7 @@ def test_several_messages(self) -> None:
293294

294295
def test_function(self) -> None:
295296
with pytest.warns(
296-
PytestDeprecationWarning,
297+
PytestPendingDeprecationWarning,
297298
match=(
298299
wrap_escape(
299300
"The callable form of pytest.warns is deprecated.\n"
@@ -452,16 +453,17 @@ def test_none_of_multiple_warns(self) -> None:
452453
warnings.warn("bbbbbbbbbb", UserWarning)
453454
warnings.warn("cccccccccc", UserWarning)
454455

455-
@pytest.mark.filterwarnings("ignore")
456+
@pytest.mark.filterwarnings("ignore:ohai")
456457
def test_can_capture_previously_warned(self) -> None:
457458
def f() -> int:
458459
warnings.warn(UserWarning("ohai"))
459460
return 10
460461

461462
assert f() == 10
462-
assert pytest.warns(UserWarning, f) == 10
463-
assert pytest.warns(UserWarning, f) == 10
464-
assert pytest.warns(UserWarning, f) != "10" # type: ignore[comparison-overlap]
463+
with pytest.warns(PytestPendingDeprecationWarning):
464+
assert pytest.warns(UserWarning, f) == 10
465+
assert pytest.warns(UserWarning, f) == 10
466+
assert pytest.warns(UserWarning, f) != "10" # type: ignore[comparison-overlap]
465467

466468
def test_warns_context_manager_with_kwargs(self) -> None:
467469
with pytest.raises(TypeError) as excinfo:

0 commit comments

Comments
 (0)