MCPcopy
hub / github.com/pytest-dev/pytest / RaisesExc

Class RaisesExc

src/_pytest/raises.py:532–720  ·  view source on GitHub ↗

.. versionadded:: 8.4 This is the class constructed when calling :func:`pytest.raises`, but may be used directly as a helper class with :class:`RaisesGroup` when you want to specify requirements on sub-exceptions. You don't need this if you only want to specify the type, sinc

Source from the content-addressed store, hash-verified

530
531@final
532class RaisesExc(AbstractRaises[BaseExcT_co_default]):
533 """
534 .. versionadded:: 8.4
535
536
537 This is the class constructed when calling :func:`pytest.raises`, but may be used
538 directly as a helper class with :class:`RaisesGroup` when you want to specify
539 requirements on sub-exceptions.
540
541 You don't need this if you only want to specify the type, since :class:`RaisesGroup`
542 accepts ``type[BaseException]``.
543
544 :param type[BaseException] | tuple[type[BaseException]] | None expected_exception:
545 The expected type, or one of several possible types.
546 May be ``None`` in order to only make use of ``match`` and/or ``check``
547
548 The type is checked with :func:`isinstance`, and does not need to be an exact match.
549 If that is wanted you can use the ``check`` parameter.
550
551 :kwparam str | Pattern[str] match:
552 A regex to match.
553
554 :kwparam Callable[[BaseException], bool] check:
555 If specified, a callable that will be called with the exception as a parameter
556 after checking the type and the match regex if specified.
557 If it returns ``True`` it will be considered a match, if not it will
558 be considered a failed match.
559
560 :meth:`RaisesExc.matches` can also be used standalone to check individual exceptions.
561
562 Examples::
563
564 with RaisesGroup(RaisesExc(ValueError, match="string"))
565 ...
566 with RaisesGroup(RaisesExc(check=lambda x: x.args == (3, "hello"))):
567 ...
568 with RaisesGroup(RaisesExc(check=lambda x: type(x) is ValueError)):
569 ...
570 """
571
572 # Trio bundled hypothesis monkeypatching, we will probably instead assume that
573 # hypothesis will handle that in their pytest plugin by the time this is released.
574 # Alternatively we could add a version of get_pretty_function_description ourselves
575 # https://github.com/HypothesisWorks/hypothesis/blob/8ced2f59f5c7bea3344e35d2d53e1f8f8eb9fcd8/hypothesis-python/src/hypothesis/internal/reflection.py#L439
576
577 # At least one of the three parameters must be passed.
578 @overload
579 def __init__(
580 self,
581 expected_exception: (
582 type[BaseExcT_co_default] | tuple[type[BaseExcT_co_default], ...]
583 ),
584 /,
585 *,
586 match: str | Pattern[str] | None = ...,
587 check: Callable[[BaseExcT_co_default], bool] | None = ...,
588 ) -> None: ...
589

Callers 15

check_raisesexc_initFunction · 0.90
test_raises_groupFunction · 0.90
test_matchesFunction · 0.90
test_messageFunction · 0.90
test_assert_messageFunction · 0.90

Calls

no outgoing calls

Tested by 15

test_raises_groupFunction · 0.72
test_matchesFunction · 0.72
test_messageFunction · 0.72
test_assert_messageFunction · 0.72
test_misordering_exampleFunction · 0.72
test_identity_oopsiesFunction · 0.72
test_raisesexcFunction · 0.72
test_raisesexc_matchFunction · 0.72