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
3 changed files with 15 additions and 12 deletions

View File

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