Check whether a captured exception group contains a matching exception. :param Type[BaseException] | Tuple[Type[BaseException]] expected_exception: The expected exception type, or a tuple if one of multiple possible exception types are expected. :param str |
(
self,
expected_exception: EXCEPTION_OR_MORE,
*,
match: str | re.Pattern[str] | None = None,
depth: int | None = None,
)
| 823 | return False |
| 824 | |
| 825 | def group_contains( |
| 826 | self, |
| 827 | expected_exception: EXCEPTION_OR_MORE, |
| 828 | *, |
| 829 | match: str | re.Pattern[str] | None = None, |
| 830 | depth: int | None = None, |
| 831 | ) -> bool: |
| 832 | """Check whether a captured exception group contains a matching exception. |
| 833 | |
| 834 | :param Type[BaseException] | Tuple[Type[BaseException]] expected_exception: |
| 835 | The expected exception type, or a tuple if one of multiple possible |
| 836 | exception types are expected. |
| 837 | |
| 838 | :param str | re.Pattern[str] | None match: |
| 839 | If specified, a string containing a regular expression, |
| 840 | or a regular expression object, that is tested against the string |
| 841 | representation of the exception and its `PEP-678 <https://peps.python.org/pep-0678/>` `__notes__` |
| 842 | using :func:`re.search`. |
| 843 | |
| 844 | To match a literal string that may contain :ref:`special characters |
| 845 | <re-syntax>`, the pattern can first be escaped with :func:`re.escape`. |
| 846 | |
| 847 | :param Optional[int] depth: |
| 848 | If `None`, will search for a matching exception at any nesting depth. |
| 849 | If >= 1, will only match an exception if it's at the specified depth (depth = 1 being |
| 850 | the exceptions contained within the topmost exception group). |
| 851 | |
| 852 | .. versionadded:: 8.0 |
| 853 | |
| 854 | .. warning:: |
| 855 | This helper makes it easy to check for the presence of specific exceptions, |
| 856 | but it is very bad for checking that the group does *not* contain |
| 857 | *any other exceptions*. |
| 858 | You should instead consider using :class:`pytest.RaisesGroup` |
| 859 | |
| 860 | """ |
| 861 | msg = "Captured exception is not an instance of `BaseExceptionGroup`" |
| 862 | assert isinstance(self.value, BaseExceptionGroup), msg |
| 863 | msg = "`depth` must be >= 1 if specified" |
| 864 | assert (depth is None) or (depth >= 1), msg |
| 865 | return self._group_contains(self.value, expected_exception, match, depth) |
| 866 | |
| 867 | |
| 868 | # Type alias for the `tbfilter` setting: |