-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Have pytest.raises match against exception __notes__
#11227
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
Conversation
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.
Looks good to me, thanks again Isaac! Once those todos are done I'd be happy to merge.
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.
LGTM, left some simple suggestions.
src/_pytest/python_api.py
Outdated
@@ -843,6 +843,13 @@ def raises( # noqa: F811 | |||
>>> with pytest.raises(ValueError, match=r'must be \d+$'): | |||
... raise ValueError("value must be 42") | |||
|
|||
The ``match`` argument searches the formatted exception string, which includes any ``__notes__``: |
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.
Perhaps it would be nice to add a link to the relevant PEP?
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.
Added!
Co-authored-by: Bruno Oliveira <[email protected]>
for more information, see https://pre-commit.ci
…ytest into match-exception-notes
Thanks for the quick review! Addressed comments, finished up, and added a couple more test cases. |
@@ -704,7 +704,12 @@ def match(self, regexp: Union[str, Pattern[str]]) -> "Literal[True]": | |||
If it matches `True` is returned, otherwise an `AssertionError` is raised. | |||
""" | |||
__tracebackhide__ = True | |||
value = str(self.value) | |||
value = "\n".join( |
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.
We might want to work with @agronholm to ensure note helpers in the backport as well as shared formatting as follow up
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.
The tests here don't seem to involve exception groups though.
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.
@agronholm my impression is that exception notes are closely related and my understanding is that integration with the backport may be helpful as per agronholm/exceptiongroup#31 and since formatting may also be affected my impression is that the most benefit for library users would come from having one integration place instead of multiple
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.
About formatting: I don't think there is a function in traceback that provides the exact rendering pytest expects. I think all the methods that traceback
provides will include the exception type.
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.
Im happy now that the design decision is done and we will use match all
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.
Nice, thanks @ivirshup!
Closes #11223
Adds support for matching against
__notes__
added to an exception withinpytest.raises
. See issue for discussion/ design.Notes
add_note
is only available in 3.11+exceptiongroup
: Add note helper? agronholm/exceptiongroup#31TODO
AUTHORS
in alphabeticalchangelog
folder, with a name like<ISSUE NUMBER>.<TYPE>.rst
.