Skip to content

Commit 3f1fb62

Browse files
committed
Rework ExceptionInfo to not require manual __init__ call
Mypy doesn't like calling __init__() in this way.
1 parent 14bf4cd commit 3f1fb62

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/_pytest/_code/code.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ def for_later(cls) -> "ExceptionInfo[_E]":
432432
"""
433433
return cls(None)
434434

435+
def fill_unfilled(self, exc_info: Tuple["Type[_E]", _E, TracebackType]) -> None:
436+
"""fill an unfilled ExceptionInfo created with for_later()"""
437+
assert self._excinfo is None, "ExceptionInfo was already filled"
438+
self._excinfo = exc_info
439+
435440
@property
436441
def type(self) -> "Type[_E]":
437442
"""the exception class"""

src/_pytest/python_api.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,13 @@ def __exit__(
745745
if exc_type is None:
746746
fail(self.message)
747747
assert self.excinfo is not None
748-
# Type ignored because mypy doesn't like calling __init__ directly like this.
749-
self.excinfo.__init__((exc_type, exc_val, exc_tb)) # type: ignore
750-
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
751-
if self.match_expr is not None and suppress_exception:
748+
if not issubclass(exc_type, self.expected_exception):
749+
return False
750+
# Cast to narrow the exception type now that it's verified.
751+
exc_info = cast(
752+
Tuple["Type[_E]", _E, TracebackType], (exc_type, exc_val, exc_tb)
753+
)
754+
self.excinfo.fill_unfilled(exc_info)
755+
if self.match_expr is not None:
752756
self.excinfo.match(self.match_expr)
753-
return suppress_exception
757+
return True

0 commit comments

Comments
 (0)