-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Avoid rechecking RaisesGroup callbacks #14327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed ``RaisesGroup.matches()`` so group-level ``check=`` callbacks are no longer called again on the first nested exception while building suggestion text. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -417,7 +417,7 @@ def is_exc(e: ExceptionGroup[ValueError]) -> bool: | |
|
|
||
| with ( | ||
| fails_raises_group( | ||
| f"check {is_exc_repr} did not return True on the ExceptionGroup" | ||
| f"check {is_exc_repr} did not return True on the ExceptionGroup. If you meant to check the sub-exception instead of the group, you might want RaisesGroup(RaisesExc(ValueError, check=<...>))" | ||
| ), | ||
| RaisesGroup(ValueError, check=is_exc), | ||
| ): | ||
|
|
@@ -429,13 +429,49 @@ def is_value_error(e: BaseException) -> bool: | |
| # helpful suggestion if the user thinks the check is for the sub-exception | ||
| with ( | ||
| fails_raises_group( | ||
| f"check {is_value_error} did not return True on the ExceptionGroup, but did return True for the expected ValueError. You might want RaisesGroup(RaisesExc(ValueError, check=<...>))" | ||
| f"check {is_value_error} did not return True on the ExceptionGroup. If you meant to check the sub-exception instead of the group, you might want RaisesGroup(RaisesExc(ValueError, check=<...>))" | ||
| ), | ||
| RaisesGroup(ValueError, check=is_value_error), | ||
| ): | ||
| raise ExceptionGroup("", (ValueError(),)) | ||
|
|
||
|
|
||
| def test_check_is_only_called_on_the_group() -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests feel perhaps slightly overkill, and a overly verbose to split them when the only thing differing is whether it uses They also don't check the number of calls to But they seem fine enough. |
||
| calls: list[type[BaseException]] = [] | ||
|
|
||
| def check(exc_group: ExceptionGroup[ValueError]) -> bool: | ||
| calls.append(type(exc_group)) | ||
| return False | ||
|
|
||
| with ( | ||
| fails_raises_group( | ||
| f"check {repr_callable(check)} did not return True on the ExceptionGroup. If you meant to check the sub-exception instead of the group, you might want RaisesGroup(RaisesExc(ValueError, check=<...>))" | ||
| ), | ||
| RaisesGroup(ValueError, check=check), | ||
| ): | ||
| raise ExceptionGroup("", (ValueError(),)) | ||
|
|
||
| assert calls == [ExceptionGroup] | ||
|
|
||
|
|
||
| def test_matches_check_is_only_called_on_the_group() -> None: | ||
| calls: list[type[BaseException]] = [] | ||
|
|
||
| def check(exc_group: ExceptionGroup[ValueError]) -> bool: | ||
| calls.append(type(exc_group)) | ||
| return False | ||
|
|
||
| matcher = RaisesGroup(ValueError, check=check) | ||
|
|
||
| assert not matcher.matches(ExceptionGroup("", (ValueError(),))) | ||
| assert matcher.fail_reason == ( | ||
| f"check {repr_callable(check)} did not return True on the ExceptionGroup. " | ||
| "If you meant to check the sub-exception instead of the group, you might " | ||
| "want RaisesGroup(RaisesExc(ValueError, check=<...>))" | ||
| ) | ||
| assert calls == [ExceptionGroup] | ||
|
|
||
|
|
||
| def test_unwrapped_match_check() -> None: | ||
| def my_check(e: object) -> bool: # pragma: no cover | ||
| return True | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise this test doesn't make any sense anymore.