@@ -113,6 +113,9 @@ def raises(
113
113
exception types are expected. Note that subclasses of the passed exceptions
114
114
will also match.
115
115
116
+ This is not a required parameter, you may opt to only use ``match`` and/or
117
+ ``check`` for verifying the raised exception.
118
+
116
119
:kwparam str | re.Pattern[str] | None match:
117
120
If specified, a string containing a regular expression,
118
121
or a regular expression object, that is tested against the string
@@ -127,6 +130,13 @@ def raises(
127
130
When using ``pytest.raises`` as a function, you can use:
128
131
``pytest.raises(Exc, func, match="passed on").match("my pattern")``.)
129
132
133
+ :kwparam Callable[[BaseException], bool] check
134
+ If specified, a callable that will be called with the exception as a parameter
135
+ after checking the type and the match regex if specified.
136
+ If it returns ``True`` it will be considered a match, if not it will
137
+ be considered a failed match.
138
+
139
+
130
140
Use ``pytest.raises`` as a context manager, which will capture the exception of the given
131
141
type, or any of its subclasses::
132
142
@@ -526,13 +536,27 @@ class RaisesExc(AbstractRaises[BaseExcT_co_default]):
526
536
"""
527
537
.. versionadded:: 8.4
528
538
529
- Helper class to be used together with RaisesGroup when you want to specify requirements on sub-exceptions.
539
+
540
+ This is the class constructed when calling :func:`pytest.raises`, but may be used
541
+ directly as a helper class with :class:`RaisesGroup` when you want to specify
542
+ requirements on sub-exceptions.
530
543
531
544
You don't need this if you only want to specify the type, since :class:`RaisesGroup`
532
545
accepts ``type[BaseException]``.
533
546
534
- The type is checked with :func:`isinstance`, and does not need to be an exact match.
535
- If that is wanted you can use the ``check`` parameter.
547
+ :param type[BaseException] | tuple[type[BaseException]] | None expected_exception:
548
+ The expected type, or one of several possible types.
549
+ May be ``None`` in order to only make use of ``match`` and/or ``check``
550
+
551
+ The type is checked with :func:`isinstance`, and does not need to be an exact match.
552
+ If that is wanted you can use the ``check`` parameter.
553
+ :kwparam str | Pattern[str] match
554
+ A regex to match.
555
+ :kwparam Callable[[BaseException], bool] check
556
+ If specified, a callable that will be called with the exception as a parameter
557
+ after checking the type and the match regex if specified.
558
+ If it returns ``True`` it will be considered a match, if not it will
559
+ be considered a failed match.
536
560
537
561
:meth:`RaisesExc.matches` can also be used standalone to check individual exceptions.
538
562
@@ -695,49 +719,77 @@ class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
695
719
:meth:`ExceptionInfo.group_contains` also tries to handle exception groups,
696
720
but it is very bad at checking that you *didn't* get unexpected exceptions.
697
721
698
-
699
722
The catching behaviour differs from :ref:`except* <except_star>`, being much
700
723
stricter about the structure by default.
701
724
By using ``allow_unwrapped=True`` and ``flatten_subgroups=True`` you can match
702
725
:ref:`except* <except_star>` fully when expecting a single exception.
703
726
704
- #. All specified exceptions must be present, *and no others*.
705
-
706
- * If you expect a variable number of exceptions you need to use
707
- :func:`pytest.raises(ExceptionGroup) <pytest.raises>` and manually check
708
- the contained exceptions. Consider making use of :meth:`RaisesExc.matches`.
727
+ :param args:
728
+ Any number of exception types, :class:`RaisesGroup` or :class:`RaisesExc`
729
+ to specify the exceptions contained in this exception.
730
+ All specified exceptions must be present in the raised group, *and no others*.
709
731
710
- #. It will only catch exceptions wrapped in an exceptiongroup by default.
732
+ If you expect a variable number of exceptions you need to use
733
+ :func:`pytest.raises(ExceptionGroup) <pytest.raises>` and manually check
734
+ the contained exceptions. Consider making use of :meth:`RaisesExc.matches`.
711
735
712
- * With ``allow_unwrapped=True`` you can specify a single expected exception (or :class:`RaisesExc`) and it will
713
- match the exception even if it is not inside an :exc:`ExceptionGroup`.
714
- If you expect one of several different exception types you need to use a :class:`RaisesExc` object.
715
-
716
- #. By default it cares about the full structure with nested :exc:`ExceptionGroup`'s. You can specify nested
717
- :exc:`ExceptionGroup`'s by passing :class:`RaisesGroup` objects as expected exceptions.
736
+ It does not care about the order of the exceptions, so
737
+ ``RaisesGroup(ValueError, TypeError)``
738
+ is equivalent to
739
+ ``RaisesGroup(TypeError, ValueError)``.
740
+ :kwparam str | re.Pattern[str] | None match:
741
+ If specified, a string containing a regular expression,
742
+ or a regular expression object, that is tested against the string
743
+ representation of the exception group and its :pep:`678` `__notes__`
744
+ using :func:`re.search`.
718
745
719
- * With ``flatten_subgroups=True`` it will "flatten" the raised :exc:`ExceptionGroup`,
720
- extracting all exceptions inside any nested :exc:`ExceptionGroup`, before matching .
746
+ To match a literal string that may contain :ref:`special characters
747
+ <re-syntax>`, the pattern can first be escaped with :func:`re.escape` .
721
748
722
- It does not care about the order of the exceptions, so
723
- ``RaisesGroup(ValueError, TypeError)``
724
- is equivalent to
725
- ``RaisesGroup(TypeError, ValueError)``.
749
+ Note that " (5 subgroups)" will be stripped from the ``repr`` before matching.
750
+ :kwparam Callable[[E], bool] check:
751
+ If specified, a callable that will be called with the group as a parameter
752
+ after successfully matching the expected exceptions. If it returns ``True``
753
+ it will be considered a match, if not it will be considered a failed match.
754
+ :kwparam bool allow_unwrapped:
755
+ If expecting a single exception or :class:`RaisesExc` it will match even
756
+ if the exception is not inside an exceptiongroup.
757
+
758
+ Using this together with ``match``, ``check`` or expecting multiple exceptions
759
+ will raise an error.
760
+ :kwparam bool flatten_subgroups:
761
+ "flatten" any groups inside the raised exception group, extracting all exceptions
762
+ inside any nested groups, before matching. Without this it expects you to
763
+ fully specify the nesting structure by passing :class:`RaisesGroup` as expected
764
+ parameter.
726
765
727
766
Examples::
728
767
729
768
with RaisesGroup(ValueError):
730
769
raise ExceptionGroup("", (ValueError(),))
770
+ # match
731
771
with RaisesGroup(
732
- ValueError, ValueError, RaisesExc(TypeError, match="expected int")
772
+ ValueError,
773
+ ValueError,
774
+ RaisesExc(TypeError, match="^expected int$"),
775
+ match="^my group$",
733
776
):
734
- ...
777
+ raise ExceptionGroup(
778
+ "my group",
779
+ [
780
+ ValueError(),
781
+ TypeError("expected int"),
782
+ ValueError(),
783
+ ],
784
+ )
785
+ # check
735
786
with RaisesGroup(
736
787
KeyboardInterrupt,
737
- match="hello",
738
- check=lambda x: type(x) is BaseExceptionGroup ,
788
+ match="^ hello$ ",
789
+ check=lambda x: isinstance(x.__cause__, ValueError) ,
739
790
):
740
- ...
791
+ raise BaseExceptionGroup("hello", [KeyboardInterrupt()]) from ValueError
792
+ # nested groups
741
793
with RaisesGroup(RaisesGroup(ValueError)):
742
794
raise ExceptionGroup("", (ExceptionGroup("", (ValueError(),)),))
743
795
0 commit comments