Allow creating ExceptionInfo from existing exc_info for better typing

This way the ExceptionInfo generic parameter can be inferred from the
passed-in exc_info. See for example the replaced cast().
This commit is contained in:
Ran Benita
2019-07-14 11:36:33 +03:00
parent 3f1fb62584
commit 11f1f79222
3 changed files with 47 additions and 20 deletions

View File

@@ -707,11 +707,11 @@ def raises(
)
try:
func(*args[1:], **kwargs)
except expected_exception:
# Cast to narrow the type to expected_exception (_E).
return cast(
_pytest._code.ExceptionInfo[_E],
_pytest._code.ExceptionInfo.from_current(),
except expected_exception as e:
# We just caught the exception - there is a traceback.
assert e.__traceback__ is not None
return _pytest._code.ExceptionInfo.from_exc_info(
(type(e), e, e.__traceback__)
)
fail(message)