From 3f1fb625844a38871d7bc357d290f39ce87039ca Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 11 Jul 2019 14:36:13 +0300 Subject: [PATCH] Rework ExceptionInfo to not require manual __init__ call Mypy doesn't like calling __init__() in this way. --- src/_pytest/_code/code.py | 5 +++++ src/_pytest/python_api.py | 14 +++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 203e90287..07ed8066c 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -432,6 +432,11 @@ class ExceptionInfo(Generic[_E]): """ 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 def type(self) -> "Type[_E]": """the exception class""" diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 7ca545878..e3cb8a970 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -745,9 +745,13 @@ class RaisesContext(Generic[_E]): if exc_type is None: fail(self.message) assert self.excinfo is not None - # Type ignored because mypy doesn't like calling __init__ directly like this. - self.excinfo.__init__((exc_type, exc_val, exc_tb)) # type: ignore - suppress_exception = issubclass(self.excinfo.type, self.expected_exception) - if self.match_expr is not None and suppress_exception: + if not issubclass(exc_type, self.expected_exception): + return False + # Cast to narrow the exception type now that it's verified. + 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) - return suppress_exception + return True