Skip to content

Commit c011e9b

Browse files
committed
fix test, fix references in docstrings
1 parent e73c411 commit c011e9b

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

doc/en/reference/reference.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,12 +1020,17 @@ RaisesExc
10201020
.. autoclass:: pytest.RaisesExc()
10211021
:members:
10221022

1023+
.. autoattribute:: fail_reason
1024+
10231025
RaisesGroup
10241026
~~~~~~~~~~~
1027+
**Tutorial**: :ref:`assert-matching-exception-groups`
10251028

10261029
.. autoclass:: pytest.RaisesGroup()
10271030
:members:
10281031

1032+
.. autoattribute:: fail_reason
1033+
10291034
TerminalReporter
10301035
~~~~~~~~~~~~~~~~
10311036

src/_pytest/_raises_group.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ def __init__(
126126

127127
@property
128128
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."""
132132
return self._fail_reason
133133

134134
def _check_check(
@@ -170,17 +170,20 @@ def matches(
170170
self: AbstractRaises[BaseExcT_1], exc_val: BaseException
171171
) -> TypeGuard[BaseExcT_1]:
172172
"""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.
174174
"""
175175

176176

177177
@final
178178
class RaisesExc(AbstractRaises[BaseExcT_co_default]):
179179
"""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.
183185
If that is wanted you can use the ``check`` parameter.
186+
184187
:meth:`RaisesExc.matches` can also be used standalone to check individual exceptions.
185188
186189
Examples::
@@ -193,7 +196,7 @@ class RaisesExc(AbstractRaises[BaseExcT_co_default]):
193196
...
194197
195198
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.
197200
"""
198201

199202
# Trio bundled hypothesis monkeypatching, we will probably instead assume that
@@ -241,8 +244,8 @@ def matches(
241244
self,
242245
exception: BaseException,
243246
) -> 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.
246249
247250
Examples::
248251
@@ -287,33 +290,34 @@ def _check_type(self, exception: BaseException) -> TypeGuard[BaseExcT_co_default
287290

288291
@final
289292
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.
294297
295298
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.
298301
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.
300303
301304
#. All specified exceptions must be present, *and no others*.
302305
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`.
305309
306310
#. It will only catch exceptions wrapped in an exceptiongroup by default.
307311
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.
311315
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.
314318
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.
317321
318322
It does not care about the order of the exceptions, so
319323
``RaisesGroups(ValueError, TypeError)``
@@ -346,7 +350,7 @@ class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
346350
raise ValueError
347351
348352
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.
350354
351355
352356
The matching algorithm is greedy, which means cases such as this may fail::
@@ -355,10 +359,10 @@ class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
355359
raise ExceptionGroup("", (ValueError("hello"), ValueError("goodbye")))
356360
357361
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.
359363
360364
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.
362366
"""
363367

364368
# allow_unwrapped=True requires: singular exception, exception not being

testing/python/raises_group.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ def test_assert_matches() -> None:
11441144
def test_xfail_raisesgroup(pytester: Pytester) -> None:
11451145
pytester.makepyfile(
11461146
"""
1147+
import sys
11471148
import pytest
11481149
if sys.version_info < (3, 11):
11491150
from exceptiongroup import ExceptionGroup

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ allowlist_externals =
107107
git
108108
commands =
109109
# Retrieve possibly missing commits:
110-
#-git fetch --unshallow
111-
#-git fetch --tags
110+
-git fetch --unshallow
111+
-git fetch --tags
112112

113113
sphinx-build \
114114
-j auto \

0 commit comments

Comments
 (0)