Skip to content

Commit 4f905c2

Browse files
Deprecate testing.RaisesGroup and testing.Matcher (#3337)
* chore: deprecate `RaisesGroup` * feat: deprecate `RaisesGroup` using `deprecate_attributes` utility * feat: deprecate `Matcher` * feat: make `DeprecatedAttribute` as `final` * feat: supress warnings about deprecation warning on specific test cases * fix: replace RaisesGroup & Matcher with pytest alternatives across all test cases * docs: add newsfragment * fix: change version number * style: format merged code * fix: adjust `test_static_tool_sees_class_members` for deprecated classes (RaisesGroup, Matcher) * fix: change text formatting, so that build tool won't search for references * fix: add underscore prefix on class names to match exported classes' names * style: correct formatting on newsfragment * fix: find/replace * fix: use `pytest.deprecated_call` context manager * fix: delete type tests, mypy errors * fix: remove deprecated calls * chore: update `_check_type_completeness.json` * test: add test case to test deprecation
1 parent b178251 commit 4f905c2

20 files changed

Lines changed: 154 additions & 338 deletions

newsfragments/3326.deprecated.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Both :class:`trio.testing.RaisesGroup` and :class:`trio.testing.Matcher` have been deprecated. Pytest alternatives ``pytest.RaisesGroup`` and ``pytest.RaisesExc`` (respectively) are considered correct replacement.

src/trio/_core/_tests/test_cancelled.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import trio
88
from trio import Cancelled
99
from trio.lowlevel import current_task
10-
from trio.testing import RaisesGroup, wait_all_tasks_blocked
10+
from trio.testing import wait_all_tasks_blocked
1111

1212
from .test_ki import ki_self
1313

@@ -108,7 +108,7 @@ async def failing_task(task_status: trio.TaskStatus[trio.lowlevel.Task]) -> None
108108
task_status.started(current_task())
109109
raise ValueError
110110

111-
with RaisesGroup(ValueError, TypeError):
111+
with pytest.RaisesGroup(ValueError, TypeError):
112112
async with trio.open_nursery() as nursery:
113113
fail_task = await nursery.start(failing_task)
114114
with pytest.raises(Cancelled, match=match_str.format(fail_task)):
@@ -123,7 +123,7 @@ async def failing_task(task_status: trio.TaskStatus[trio.lowlevel.Task]) -> None
123123
await wait_all_tasks_blocked()
124124
raise ValueError
125125

126-
with RaisesGroup(ValueError, TypeError):
126+
with pytest.RaisesGroup(ValueError, TypeError):
127127
async with trio.open_nursery() as nursery:
128128
fail_task = await nursery.start(failing_task)
129129
await nursery.start(cancelled_task, fail_task)
@@ -147,7 +147,7 @@ async def cancelled_task() -> None:
147147
):
148148
await trio.sleep_forever()
149149

150-
with RaisesGroup(ValueError):
150+
with pytest.RaisesGroup(ValueError):
151151
async with trio.open_nursery() as nursery:
152152
nursery.start_soon(cancelled_task)
153153
await wait_all_tasks_blocked()
@@ -192,7 +192,7 @@ async def child() -> None:
192192
):
193193
await trio.sleep_forever()
194194

195-
with RaisesGroup(ValueError):
195+
with pytest.RaisesGroup(ValueError):
196196
async with trio.open_nursery() as nursery:
197197
nursery.start_soon(child)
198198
await ev.wait()
@@ -214,7 +214,7 @@ async def sleeper(name: str) -> None:
214214
async def raiser(name: str) -> None:
215215
ki_self()
216216

217-
with RaisesGroup(KeyboardInterrupt):
217+
with pytest.RaisesGroup(KeyboardInterrupt):
218218
async with trio.open_nursery() as nursery:
219219
nursery.start_soon(sleeper, "s1")
220220
nursery.start_soon(sleeper, "s2")

src/trio/_core/_tests/test_ki.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import outcome
1313
import pytest
1414

15-
from trio.testing import RaisesGroup
16-
1715
from .tutil import gc_collect_harder
1816

1917
try:
@@ -309,7 +307,7 @@ async def check_unprotected_kill() -> None:
309307
nursery.start_soon(raiser, "r1", record_set)
310308

311309
# raises inside a nursery, so the KeyboardInterrupt is wrapped in an ExceptionGroup
312-
with RaisesGroup(KeyboardInterrupt):
310+
with pytest.RaisesGroup(KeyboardInterrupt):
313311
_core.run(check_unprotected_kill)
314312
assert record_set == {"s1 ok", "s2 ok", "r1 raise ok"}
315313

@@ -326,7 +324,7 @@ async def check_protected_kill() -> None:
326324
# __aexit__ blocks, and then receives the KI
327325

328326
# raises inside a nursery, so the KeyboardInterrupt is wrapped in an ExceptionGroup
329-
with RaisesGroup(KeyboardInterrupt):
327+
with pytest.RaisesGroup(KeyboardInterrupt):
330328
_core.run(check_protected_kill)
331329
assert record_set == {"s1 ok", "s2 ok", "r1 cancel ok"}
332330

src/trio/_core/_tests/test_parking_lot.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
current_task,
1212
remove_parking_lot_breaker,
1313
)
14-
from trio.testing import Matcher, RaisesGroup
1514

1615
from ... import _core
1716
from ...testing import wait_all_tasks_blocked
@@ -267,8 +266,8 @@ async def bad_parker(lot: ParkingLot, scope: _core.CancelScope) -> None:
267266
cs = _core.CancelScope()
268267

269268
# check that parked task errors
270-
with RaisesGroup(
271-
Matcher(_core.BrokenResourceError, match="^Parking lot broken by"),
269+
with pytest.RaisesGroup(
270+
pytest.RaisesExc(_core.BrokenResourceError, match="^Parking lot broken by"),
272271
):
273272
async with _core.open_nursery() as nursery:
274273
nursery.start_soon(bad_parker, lot, cs)
@@ -382,8 +381,8 @@ async def return_me_and_park(
382381
await lot.park()
383382

384383
lot = ParkingLot()
385-
with RaisesGroup(
386-
Matcher(_core.BrokenResourceError, match="^Parking lot broken by"),
384+
with pytest.RaisesGroup(
385+
pytest.RaisesExc(_core.BrokenResourceError, match="^Parking lot broken by"),
387386
):
388387
async with _core.open_nursery() as nursery:
389388
child_task = await nursery.start(return_me_and_park, lot)

0 commit comments

Comments
 (0)