fix: RaisesGroup check() no longer called with sub-exceptions (#14324) - #15116
Open
DawnofGenX wants to merge 3 commits into
Open
DawnofGenX wants to merge 3 commits into
DawnofGenX wants to merge 3 commits into
Conversation
…est-dev#14324) Reproduces the bug where RaisesGroup.matches calls the user's check() function a second time with a contained exception (not the group) when the group check returns False, crashing with AttributeError for checks that assume an ExceptionGroup. TDD red phase; no fix included.
…#14324) When the group-level check returned False, matches() re-invoked the user's check() with a contained exception to build a "did return True for the expected X" suggestion. This crashed checks that assume an ExceptionGroup (AttributeError) and violated the documented contract that check receives the group. The check callback is now only ever called with the group itself; the speculative sub-exception probe is removed, so the suggestion text is dropped from the failure message.
mypy reports the comment as unused; the error it suppressed no longer exists. pre-commit.ci flagged this on pytest-dev#15116.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14324.
RaisesGroup'scheckcallback was being called a second time with a contained sub-exception after the callback had already rejected the group. This came from a speculative probe used to build a nicer failure hint ("did return True for the expected ... You might wantRaisesGroup(RaisesExc(..., check=<...>))"). It:checkreceives the exception group itself,e.exceptionsraisedAttributeErroron the sub-exception and leaked out ofpytest.raisesmatching), andFix
The speculative sub-exception probe is removed entirely — not merely wrapped in
try/except. Whencheckrejects the group, the failure reason is now set directly andcheckis never invoked again. This drops the "You might wantRaisesGroup(RaisesExc(...))" hint, but restores the contract and eliminates the whole crash class.Changes
src/_pytest/raises.py— remove the sub-exception probe inRaisesGroupcheck handling.testing/python/raises_group.py— regression test assertingcheckis only ever called with the group and never crashes when it uses group-only attributes.changelog/14324.bugfix.rst— changelog entry.Test plan
test_check_not_called_with_subexception_issue_14324fails onmain(verified red:AttributeError: 'ValueError' object has no attribute 'exceptions') and passes with the fix.testing/python/raises_group.py+testing/python/raises.pypass in full (67 passed).Closes #14324