Rework ExceptionInfo to not require manual __init__ call
Mypy doesn't like calling __init__() in this way.
This commit is contained in:
parent
14bf4cdf44
commit
3f1fb62584
|
@ -432,6 +432,11 @@ class ExceptionInfo(Generic[_E]):
|
||||||
"""
|
"""
|
||||||
return cls(None)
|
return cls(None)
|
||||||
|
|
||||||
|
def fill_unfilled(self, exc_info: Tuple["Type[_E]", _E, TracebackType]) -> None:
|
||||||
|
"""fill an unfilled ExceptionInfo created with for_later()"""
|
||||||
|
assert self._excinfo is None, "ExceptionInfo was already filled"
|
||||||
|
self._excinfo = exc_info
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self) -> "Type[_E]":
|
def type(self) -> "Type[_E]":
|
||||||
"""the exception class"""
|
"""the exception class"""
|
||||||
|
|
|
@ -745,9 +745,13 @@ class RaisesContext(Generic[_E]):
|
||||||
if exc_type is None:
|
if exc_type is None:
|
||||||
fail(self.message)
|
fail(self.message)
|
||||||
assert self.excinfo is not None
|
assert self.excinfo is not None
|
||||||
# Type ignored because mypy doesn't like calling __init__ directly like this.
|
if not issubclass(exc_type, self.expected_exception):
|
||||||
self.excinfo.__init__((exc_type, exc_val, exc_tb)) # type: ignore
|
return False
|
||||||
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
|
# Cast to narrow the exception type now that it's verified.
|
||||||
if self.match_expr is not None and suppress_exception:
|
exc_info = cast(
|
||||||
|
Tuple["Type[_E]", _E, TracebackType], (exc_type, exc_val, exc_tb)
|
||||||
|
)
|
||||||
|
self.excinfo.fill_unfilled(exc_info)
|
||||||
|
if self.match_expr is not None:
|
||||||
self.excinfo.match(self.match_expr)
|
self.excinfo.match(self.match_expr)
|
||||||
return suppress_exception
|
return True
|
||||||
|
|
Loading…
Reference in New Issue