Improve doc/typing/message for `ExceptionInfo.match` (#6776)

This commit is contained in:
Daniel Hahler 2020-02-21 16:41:57 +01:00 committed by GitHub
parent 8e991a622c
commit 2be06ba67e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 12 deletions

View File

@ -633,17 +633,18 @@ class ExceptionInfo(Generic[_E]):
) )
return fmt.repr_excinfo(self) return fmt.repr_excinfo(self)
def match(self, regexp: "Union[str, Pattern]") -> bool: def match(self, regexp: "Union[str, Pattern]") -> "Literal[True]":
""" """
Check whether the regular expression 'regexp' is found in the string Check whether the regular expression `regexp` matches the string
representation of the exception using ``re.search``. If it matches representation of the exception using :func:`python:re.search`.
then True is returned (so that it is possible to write If it matches `True` is returned.
``assert excinfo.match()``). If it doesn't match an AssertionError is If it doesn't match an `AssertionError` is raised.
raised.
""" """
__tracebackhide__ = True __tracebackhide__ = True
if not re.search(regexp, str(self.value)): assert re.search(
assert 0, "Pattern {!r} not found in {!r}".format(regexp, str(self.value)) regexp, str(self.value)
), "Pattern {!r} does not match {!r}".format(regexp, str(self.value))
# Return True to allow for "assert excinfo.match()".
return True return True

View File

@ -411,13 +411,15 @@ def test_match_raises_error(testdir):
) )
result = testdir.runpytest() result = testdir.runpytest()
assert result.ret != 0 assert result.ret != 0
result.stdout.fnmatch_lines(["*AssertionError*Pattern*[123]*not found*"])
exc_msg = "Pattern '[[]123[]]+' does not match 'division by zero'"
result.stdout.fnmatch_lines(["E * AssertionError: {}".format(exc_msg)])
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*") result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")
result = testdir.runpytest("--fulltrace") result = testdir.runpytest("--fulltrace")
assert result.ret != 0 assert result.ret != 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*__tracebackhide__ = True*", "*AssertionError*Pattern*[123]*not found*"] ["*__tracebackhide__ = True*", "E * AssertionError: {}".format(exc_msg)]
) )

View File

@ -194,7 +194,7 @@ class TestRaises:
int("asdf") int("asdf")
msg = "with base 16" msg = "with base 16"
expr = r"Pattern '{}' not found in \"invalid literal for int\(\) with base 10: 'asdf'\"".format( expr = r"Pattern '{}' does not match \"invalid literal for int\(\) with base 10: 'asdf'\"".format(
msg msg
) )
with pytest.raises(AssertionError, match=expr): with pytest.raises(AssertionError, match=expr):
@ -206,7 +206,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 msg == 'Pattern "\'foo" not found in "\'bar"' assert msg == 'Pattern "\'foo" does not match "\'bar"'
def test_raises_match_wrong_type(self): def test_raises_match_wrong_type(self):
"""Raising an exception with the wrong type and match= given. """Raising an exception with the wrong type and match= given.