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
Enhance errors for exception/warnings matching #8508
Conversation
f5f0b12
to
9000f94
Compare
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.
Might want to use f-strings instead of str.format
everywhere?
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.
Oh, also, why the extra (unneeded) assert
before excinfo.match
?
@The-Compiler interesting, pycharm seems to have added those i didn't even note, going to take them off |
f679304
to
c7328e5
Compare
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, thanks!
We might want to add a small changelog entry 8508.improvement.rst
:
Improve :func:`pytest.raises` and :func:`pytest.warns` error messages.
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, just a (preexisting) grammar fix and a suggestion.
msg += " Did you mean to `re.escape()` the regex?" | ||
assert re.search(regexp, str(self.value)), msg.format(regexp, str(self.value)) | ||
value = str(self.value) | ||
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}" |
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.
I wonder if it wouldn't be better to use a straight str here instead of repr. With the new line format the quoting doesn't seem needed, perhaps the opposite.
(Either way is fine with me)
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.
i believe for the regex it helps to get the escapes printed more nicely readable, my personal opinion is that repr reads slightly better than just the str or the escape
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.
Just to clarify, this will show as
>>> regexp = r'py\.test'
>>> value = 'pytest'
>>> msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
>>> print(msg)
Regex pattern did not match.
Regex: 'py\\.test'
Input: 'pytest'
I would have preferred py\.test
over py\\.test
.
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.
i just experimented with this - if there is a simple way to print "raw" strings, i'll happily use that - however even in the pytest testsuite there is a number of cases that would turn confusing without
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.
if there is a simple way to print "raw" strings, i'll happily use that
That would be {regexp}
instead of {regexp!r}
, unless I'm misunderstanding.
however even in the pytest testsuite there is a number of cases that would turn confusing without
Can you give an example?
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.
Hmm, I'm somewhat undecided here. I agree the double backslashes are a bit confusing, but at the same time I think it's good to print the repr for the input (considering that it can contain whitespace and such, and seeing that whitespace might be essential to find out why the pattern doesn't match).
But then if we use the repr for the input, it might be more consistent and thus less confusing if we use the repr for the pattern as well.
c7328e5
to
3e6ab2d
Compare
3e6ab2d
to
096c043
Compare
211ea2b
to
a212945
Compare
9d8a0e1
to
7db2330
Compare
7db2330
to
13e52e0
Compare
a47b971
to
62107fb
Compare
@The-Compiler @nicoddemus i believe this is complete niw, please have another look |
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, thanks!
msg += " Did you mean to `re.escape()` the regex?" | ||
assert re.search(regexp, str(self.value)), msg.format(regexp, str(self.value)) | ||
value = str(self.value) | ||
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}" |
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.
Just to clarify, this will show as
>>> regexp = r'py\.test'
>>> value = 'pytest'
>>> msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
>>> print(msg)
Regex pattern did not match.
Regex: 'py\\.test'
Input: 'pytest'
I would have preferred py\.test
over py\\.test
.
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.
Couple things I noticed about the warns error message. They are preexisting so not meant to block this PR, just if you feel like it.
62107fb
to
a6da838
Compare
c9e312e
to
06d2435
Compare
06d2435
to
53c2331
Compare
53c2331
to
282e030
Compare
* use f-strings more * use pformat to ensure multi line print for longer-warning lists while keeping short ones small
* now multiline and compares regexes * warns on forgotten escaping
282e030
to
995d833
Compare
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.
Some nice cleanups and output improvements! I added my $0.02 to a couple of things. Nothing blocking, though.
msg += " Did you mean to `re.escape()` the regex?" | ||
assert re.search(regexp, str(self.value)), msg.format(regexp, str(self.value)) | ||
value = str(self.value) | ||
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}" |
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.
Hmm, I'm somewhat undecided here. I agree the double backslashes are a bit confusing, but at the same time I think it's good to print the repr for the input (considering that it can contain whitespace and such, and seeing that whitespace might be essential to find out why the pattern doesn't match).
But then if we use the repr for the input, it might be more consistent and thus less confusing if we use the repr for the pattern as well.
Co-authored-by: Florian Bruhin <me@the-compiler.org>
No description provided.