@@ -126,9 +126,9 @@ def __init__(
126
126
127
127
@property
128
128
def fail_reason (self ) -> str | None :
129
- """Set after a call to `matches` to give a human-readable reason for why the match failed.
130
- When used as a context manager the string will be given as the text of an
131
- `Failed` """
129
+ """Set after a call to :meth: `matches` to give a human-readable reason for why the match failed.
130
+ When used as a context manager the string will be printed as the reason for the
131
+ test failing. """
132
132
return self ._fail_reason
133
133
134
134
def _check_check (
@@ -170,17 +170,20 @@ def matches(
170
170
self : AbstractRaises [BaseExcT_1 ], exc_val : BaseException
171
171
) -> TypeGuard [BaseExcT_1 ]:
172
172
"""Check if an exception matches the requirements of this AbstractRaises.
173
- If it fails, `AbstractRaises.fail_reason` should be set.
173
+ If it fails, :meth: `AbstractRaises.fail_reason` should be set.
174
174
"""
175
175
176
176
177
177
@final
178
178
class RaisesExc (AbstractRaises [BaseExcT_co_default ]):
179
179
"""Helper class to be used together with RaisesGroups when you want to specify requirements on sub-exceptions.
180
- Only specifying the type is redundant, and it's also unnecessary when the type is a
181
- nested `RaisesGroup` since it supports the same arguments.
182
- The type is checked with `isinstance`, and does not need to be an exact match.
180
+
181
+ You don't need this if you only want to specify the type, since :class:`RaisesGroup`
182
+ accepts ``type[BaseException]``.
183
+
184
+ The type is checked with :func:`isinstance`, and does not need to be an exact match.
183
185
If that is wanted you can use the ``check`` parameter.
186
+
184
187
:meth:`RaisesExc.matches` can also be used standalone to check individual exceptions.
185
188
186
189
Examples::
@@ -193,7 +196,7 @@ class RaisesExc(AbstractRaises[BaseExcT_co_default]):
193
196
...
194
197
195
198
Tip: if you install ``hypothesis`` and import it in ``conftest.py`` you will get
196
- readable ``repr``s of ``check`` callables in the output.
199
+ readable ``repr``' s of ``check`` callables in the output.
197
200
"""
198
201
199
202
# Trio bundled hypothesis monkeypatching, we will probably instead assume that
@@ -241,8 +244,8 @@ def matches(
241
244
self ,
242
245
exception : BaseException ,
243
246
) -> TypeGuard [BaseExcT_co_default ]:
244
- """Check if an exception matches the requirements of this RaisesExc.
245
- If it fails, `RaisesExc.fail_reason` will be set.
247
+ """Check if an exception matches the requirements of this :class:` RaisesExc` .
248
+ If it fails, :attr: `RaisesExc.fail_reason` will be set.
246
249
247
250
Examples::
248
251
@@ -287,33 +290,34 @@ def _check_type(self, exception: BaseException) -> TypeGuard[BaseExcT_co_default
287
290
288
291
@final
289
292
class RaisesGroup (AbstractRaises [BaseExceptionGroup [BaseExcT_co ]]):
290
- """Contextmanager for checking for an expected `ExceptionGroup`.
291
- This works similar to `` pytest.raises`` , but allows for specifying the structure of an `ExceptionGroup`.
292
- `ExceptionInfo.group_contains` also tries to handle exception groups,
293
- but it is very bad at checking that you *didn't* get exceptions you didn't expect .
293
+ """Contextmanager for checking for an expected :exc: `ExceptionGroup`.
294
+ This works similar to :func:` pytest.raises`, but allows for specifying the structure of an :exc: `ExceptionGroup`.
295
+ :meth: `ExceptionInfo.group_contains` also tries to handle exception groups,
296
+ but it is very bad at checking that you *didn't* get unexpected exceptions .
294
297
295
298
296
- The catching behaviour differs from :ref:`except* <except_star>` in multiple
297
- different ways, being much stricter by default.
299
+ The catching behaviour differs from :ref:`except* <except_star>`, being much
300
+ stricter about the structure by default.
298
301
By using ``allow_unwrapped=True`` and ``flatten_subgroups=True`` you can match
299
- `` except*` ` fully when expecting a single exception.
302
+ :ref:` except* <except_star> ` fully when expecting a single exception.
300
303
301
304
#. All specified exceptions must be present, *and no others*.
302
305
303
- * If you expect a variable number of exceptions you need to use ``pytest.raises(ExceptionGroup)`` and manually
304
- check the contained exceptions. Consider making use of :func:`RaisesExc.matches`.
306
+ * If you expect a variable number of exceptions you need to use
307
+ :func:`pytest.raises(ExceptionGroup) <pytest.raises>` and manually check
308
+ the contained exceptions. Consider making use of :meth:`RaisesExc.matches`.
305
309
306
310
#. It will only catch exceptions wrapped in an exceptiongroup by default.
307
311
308
- * With ``allow_unwrapped=True`` you can specify a single expected exception or `RaisesExc` and it will match
309
- the exception even if it is not inside an `ExceptionGroup`.
310
- If you expect one of several different exception types you need to use a `RaisesExc` object.
312
+ * With ``allow_unwrapped=True`` you can specify a single expected exception ( or `RaisesExc`) and it will match
313
+ the exception even if it is not inside an :exc: `ExceptionGroup`.
314
+ If you expect one of several different exception types you need to use a :class: `RaisesExc` object.
311
315
312
- #. By default it cares about the full structure with nested `ExceptionGroup`'s. You can specify nested
313
- `ExceptionGroup`'s by passing `RaisesGroup` objects as expected exceptions.
316
+ #. By default it cares about the full structure with nested :exc: `ExceptionGroup`'s. You can specify nested
317
+ :exc: `ExceptionGroup`'s by passing :class: `RaisesGroup` objects as expected exceptions.
314
318
315
- * With ``flatten_subgroups=True`` it will "flatten" the raised `ExceptionGroup`,
316
- extracting all exceptions inside any nested :class :`ExceptionGroup`, before matching.
319
+ * With ``flatten_subgroups=True`` it will "flatten" the raised :exc: `ExceptionGroup`,
320
+ extracting all exceptions inside any nested :exc :`ExceptionGroup`, before matching.
317
321
318
322
It does not care about the order of the exceptions, so
319
323
``RaisesGroups(ValueError, TypeError)``
@@ -346,7 +350,7 @@ class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
346
350
raise ValueError
347
351
348
352
349
- `RaisesGroup.matches` can also be used directly to check a standalone exception group.
353
+ :meth: `RaisesGroup.matches` can also be used directly to check a standalone exception group.
350
354
351
355
352
356
The matching algorithm is greedy, which means cases such as this may fail::
@@ -355,10 +359,10 @@ class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
355
359
raise ExceptionGroup("", (ValueError("hello"), ValueError("goodbye")))
356
360
357
361
even though it generally does not care about the order of the exceptions in the group.
358
- To avoid the above you should specify the first ValueError with a RaisesExc as well.
362
+ To avoid the above you should specify the first :exc:` ValueError` with a :class:` RaisesExc` as well.
359
363
360
364
Tip: if you install ``hypothesis`` and import it in ``conftest.py`` you will get
361
- readable ``repr``s of ``check`` callables in the output.
365
+ readable ``repr``' s of ``check`` callables in the output.
362
366
"""
363
367
364
368
# allow_unwrapped=True requires: singular exception, exception not being
0 commit comments