-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Enhance errors for exception/warnings matching #8508
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
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,2 @@ | ||
Introduce multiline display for warning matching via :py:func:`pytest.warns` and | ||
enhance match comparison for :py:func:`_pytest._code.ExceptionInfo.match` as returned by :py:func:`pytest.raises`. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -672,10 +672,11 @@ def match(self, regexp: Union[str, Pattern[str]]) -> "Literal[True]": | |
If it matches `True` is returned, otherwise an `AssertionError` is raised. | ||
""" | ||
__tracebackhide__ = True | ||
msg = "Regex pattern {!r} does not match {!r}." | ||
if regexp == str(self.value): | ||
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 commentThe 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 commentThe 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 commentThe 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 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. 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 commentThe reason will be displayed to describe this comment to others. Learn more.
That would be
Can you give an example? 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. 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. |
||
if regexp == value: | ||
msg += "\n Did you mean to `re.escape()` the regex?" | ||
assert re.search(regexp, value), msg | ||
# Return True to allow for "assert excinfo.match()". | ||
return True | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.