-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Support __notes__ in pytest.raises #11223
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
Comments
We need a design decision on whether match plainly should apply to notes or if we should use a new name |
Given that the standard library's error formatting does not make it explicit which parts of the error message were from notes, I think it would make sense to match against the notes by default. |
It's not necessarily a good idea to inform checks of data details from presentation formatting |
It would be nice to get @Zac-HD's feedback, as the original PEP author. 😁 I would lean towards a new |
I have a mild preference for matching against the My specific use case is that I am doing IO on a hierarchical format. If an exception occurs, I want the path that was being operated on to be included in the error message. Tests that I am writing here include:
I don't love passing a separate pattern for the notes vs the "body". I think this tests the structure of the error, but not in a very precise way. Does this match any note? All notes? The n-th note? |
I agree with @ivirshup; it seems most natural to me for the existing match= argument to be passed the exception string including any notes. I expect more complex use cases to be very rare, and they can always inspect the attributes directly. |
Cool, thanks for the explanation. I'm 👍 then for the existing |
I've started a proof of concept here: https://github.com/ivirshup/pytest/tree/match-exception-notes Still needs:
|
|
Opened a draft PR, but happy to continue discussing design here in case any significant changes would be made. |
I would like
pytest.raises
to have some form of support for__notes__
.What's the problem this feature will solve?
I am currently looking into replacing dynamic patching of error messages with the new
__notes__
feature added in PEP-678.However, once I use
__notes__
my checks usingpytest.raises
now fail, and I'd like them to not. Or at least, I would like for pytest to have some features for matching these errors. Since this is a language level feature for python, I think it's usage should be covered bypytest
.Describe the solution you'd like
I would like the regex from
pytest.raises
to match against the notes as well as the body of the error. It would be fine for this to be specified with a keyword argument.Example of usage:
Alternative Solutions
Do nothing
I could manually check for notes, but I don't think python makes it obvious which part of an exception is from a note. Here is a small example of what a note looks like:
setup
I think this would make it quite difficult for someone receiving an exception with notes from an upstream library to know how to match against it.
I'm currently handling this with a helper function:
Additional context
This line would have to change:
pytest/src/_pytest/_code/code.py
Line 707 in b20e7f6
The text was updated successfully, but these errors were encountered: