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

Function test_unwrapped_match_check

testing/python/raises_group.py:439–467  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

437
438
439def test_unwrapped_match_check() -> None:
440 def my_check(e: object) -> bool: # pragma: no cover
441 return True
442
443 msg = (
444 "`allow_unwrapped=True` bypasses the `match` and `check` parameters"
445 " if the exception is unwrapped. If you intended to match/check the"
446 " exception you should use a `RaisesExc` object. If you want to match/check"
447 " the exceptiongroup when the exception *is* wrapped you need to"
448 " do e.g. `if isinstance(exc.value, ExceptionGroup):"
449 " assert RaisesGroup(...).matches(exc.value)` afterwards."
450 )
451 with pytest.raises(ValueError, match=re.escape(msg)):
452 RaisesGroup(ValueError, allow_unwrapped=True, match="foo") # type: ignore[call-overload]
453 with pytest.raises(ValueError, match=re.escape(msg)):
454 RaisesGroup(ValueError, allow_unwrapped=True, check=my_check) # type: ignore[call-overload]
455
456 # Users should instead use a RaisesExc
457 rg = RaisesGroup(RaisesExc(ValueError, match="^foo$"), allow_unwrapped=True)
458 with rg:
459 raise ValueError("foo")
460 with rg:
461 raise ExceptionGroup("", [ValueError("foo")])
462
463 # or if they wanted to match/check the group, do a conditional `.matches()`
464 with RaisesGroup(ValueError, allow_unwrapped=True) as exc:
465 raise ExceptionGroup("bar", [ValueError("foo")])
466 if isinstance(exc.value, ExceptionGroup): # pragma: no branch
467 assert RaisesGroup(ValueError, match="bar").matches(exc.value)
468
469
470def test_matches() -> None:

Callers

nothing calls this directly

Calls 3

RaisesGroupClass · 0.90
RaisesExcClass · 0.90
matchesMethod · 0.45

Tested by

no test coverage detected