Improvement: update pytest.raises document adding PEP-678 __notes__

This commit is contained in:
bowu 2023-07-19 08:34:39 +08:00
parent 5877bb8e8d
commit 9f37b96c97
4 changed files with 7 additions and 9 deletions

View File

@ -710,7 +710,7 @@ class ExceptionInfo(Generic[E]):
*getattr(self.value, "__notes__", []), *getattr(self.value, "__notes__", []),
] ]
) )
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n str(exception): {value!r}" msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
if regexp == value: if regexp == value:
msg += "\n Did you mean to `re.escape()` the regex?" msg += "\n Did you mean to `re.escape()` the regex?"
assert re.search(regexp, value), msg assert re.search(regexp, value), msg

View File

@ -812,7 +812,8 @@ def raises( # noqa: F811
:kwparam str | typing.Pattern[str] | None match: :kwparam str | typing.Pattern[str] | None match:
If specified, a string containing a regular expression, If specified, a string containing a regular expression,
or a regular expression object, that is tested against the string or a regular expression object, that is tested against the string
representation of the exception using :func:`re.search`. representation or `PEP-678 <https://peps.python.org/pep-0678/>` `__notes__`
of the exception using :func:`re.search`.
To match a literal string that may contain :ref:`special characters To match a literal string that may contain :ref:`special characters
<re-syntax>`, the pattern can first be escaped with :func:`re.escape`. <re-syntax>`, the pattern can first be escaped with :func:`re.escape`.

View File

@ -434,7 +434,7 @@ def test_match_raises_error(pytester: Pytester) -> None:
match = [ match = [
r"E .* AssertionError: Regex pattern did not match.", r"E .* AssertionError: Regex pattern did not match.",
r"E .* Regex: '\[123\]\+'", r"E .* Regex: '\[123\]\+'",
r"E .* str\(exception\): 'division by zero'", r"E .* Input: 'division by zero'",
] ]
result.stdout.re_match_lines(match) result.stdout.re_match_lines(match)
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*") result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")

View File

@ -196,7 +196,7 @@ class TestRaises:
expr = ( expr = (
"Regex pattern did not match.\n" "Regex pattern did not match.\n"
f" Regex: {msg!r}\n" f" Regex: {msg!r}\n"
" str(exception): \"invalid literal for int() with base 10: 'asdf'\"" " Input: \"invalid literal for int() with base 10: 'asdf'\""
) )
with pytest.raises(AssertionError, match="(?m)" + re.escape(expr)): with pytest.raises(AssertionError, match="(?m)" + re.escape(expr)):
with pytest.raises(ValueError, match=msg): with pytest.raises(ValueError, match=msg):
@ -221,10 +221,7 @@ class TestRaises:
with pytest.raises(AssertionError, match="'foo"): with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar") raise AssertionError("'bar")
(msg,) = excinfo.value.args (msg,) = excinfo.value.args
assert ( assert msg == '''Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
msg
== '''Regex pattern did not match.\n Regex: "'foo"\n str(exception): "'bar"'''
)
def test_match_failure_exact_string_message(self): def test_match_failure_exact_string_message(self):
message = "Oh here is a message with (42) numbers in parameters" message = "Oh here is a message with (42) numbers in parameters"
@ -235,7 +232,7 @@ class TestRaises:
assert msg == ( assert msg == (
"Regex pattern did not match.\n" "Regex pattern did not match.\n"
" Regex: 'Oh here is a message with (42) numbers in parameters'\n" " Regex: 'Oh here is a message with (42) numbers in parameters'\n"
" str(exception): 'Oh here is a message with (42) numbers in parameters'\n" " Input: 'Oh here is a message with (42) numbers in parameters'\n"
" Did you mean to `re.escape()` the regex?" " Did you mean to `re.escape()` the regex?"
) )