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

Method matches

src/_pytest/raises.py:631–664  ·  src/_pytest/raises.py::RaisesExc.matches

Check if an exception matches the requirements of this :class:`RaisesExc`. If it fails, :attr:`RaisesExc.fail_reason` will be set. Examples:: assert RaisesExc(ValueError).matches(my_exception): # is equivalent to assert isinstance(my_exception, V

(
        self,
        exception: BaseException | None,
    )

Source from the content-addressed store, hash-verified

629 self._just_propagate = False
630
631 def matches(
632 self,
633 exception: BaseException | None,
634 ) -> TypeGuard[BaseExcT_co_default]:
635 class="st">"""Check if an exception matches the requirements of this :class:`RaisesExc`.
636 If it fails, :attr:`RaisesExc.fail_reason` will be set.
637
638 Examples::
639
640 assert RaisesExc(ValueError).matches(my_exception):
641 class="cm"># is equivalent to
642 assert isinstance(my_exception, ValueError)
643
644 class="cm"># this can be useful when checking e.g. the ``__cause__`` of an exception.
645 with pytest.raises(ValueError) as excinfo:
646 ...
647 assert RaisesExc(SyntaxError, match=class="st">"foo").matches(excinfo.value.__cause__)
648 class="cm"># above line is equivalent to
649 assert isinstance(excinfo.value.__cause__, SyntaxError)
650 assert re.search(class="st">"foo", str(excinfo.value.__cause__)
651
652 class="st">"""
653 self._just_propagate = False
654 if exception is None:
655 self._fail_reason = class="st">"exception is None"
656 return False
657 if not self._check_type(exception):
658 self._just_propagate = True
659 return False
660
661 if not self._check_match(exception):
662 return False
663
664 return self._check_check(exception)
665
666 def __repr__(self) -> str:
667 parameters = []

Callers 9

__exit__Method · 0.95
test_matchesFunction · 0.95
_check_expectedMethod · 0.45
check_basic_matchesFunction · 0.45
test_assert_matchesFunction · 0.45

Calls 3

_check_typeMethod · 0.95
_check_matchMethod · 0.80
_check_checkMethod · 0.80

Tested by 3

test_matchesFunction · 0.76
test_assert_matchesFunction · 0.36