Stop using ExceptionInfo.errisinstance internally

It does the same as a simple isinstance check, but adds a little layer
of obscurity on top, which the type checker can't penetrate.
This commit is contained in:
Ran Benita 2020-06-28 18:34:32 +03:00
parent 2ae721cda5
commit fb2640b82f
5 changed files with 22 additions and 14 deletions

View File

@ -558,7 +558,10 @@ class ExceptionInfo(Generic[_E]):
def errisinstance(
self, exc: Union["Type[BaseException]", Tuple["Type[BaseException]", ...]]
) -> bool:
""" return True if the exception is an instance of exc """
"""Return True if the exception is an instance of exc.
Consider using ``isinstance(excinfo.value, exc)`` instead.
"""
return isinstance(self.value, exc)
def _getreprcrash(self) -> "ReprFileLocation":
@ -804,7 +807,7 @@ class FormattedExcinfo:
if self.tbfilter:
traceback = traceback.filter()
if excinfo.errisinstance(RecursionError):
if isinstance(excinfo.value, RecursionError):
traceback, extraline = self._truncate_recursive_traceback(traceback)
else:
extraline = None

View File

@ -265,7 +265,7 @@ def wrap_session(
session.exitstatus = exc.returncode
sys.stderr.write("{}: {}\n".format(type(exc).__name__, exc))
else:
if excinfo.errisinstance(SystemExit):
if isinstance(excinfo.value, SystemExit):
sys.stderr.write("mainloop: caught unexpected SystemExit!\n")
finally:

View File

@ -308,7 +308,7 @@ class TestReport(BaseReport):
if not isinstance(excinfo, ExceptionInfo):
outcome = "failed"
longrepr = excinfo
elif excinfo.errisinstance(skip.Exception):
elif isinstance(excinfo.value, skip.Exception):
outcome = "skipped"
r = excinfo._getreprcrash()
longrepr = (str(r.path), r.lineno, r.message)

View File

@ -215,11 +215,18 @@ def call_and_report(
def check_interactive_exception(call: "CallInfo", report: BaseReport) -> bool:
return call.excinfo is not None and not (
hasattr(report, "wasxfail")
or call.excinfo.errisinstance(Skipped)
or call.excinfo.errisinstance(bdb.BdbQuit)
)
"""Check whether the call raised an exception that should be reported as
interactive."""
if call.excinfo is None:
# Didn't raise.
return False
if hasattr(report, "wasxfail"):
# Exception was expected.
return False
if isinstance(call.excinfo.value, (Skipped, bdb.BdbQuit)):
# Special control flow exception.
return False
return True
def call_runtest_hook(
@ -287,7 +294,7 @@ class CallInfo(Generic[_T]):
result = func() # type: Optional[_T]
except BaseException:
excinfo = ExceptionInfo.from_current()
if reraise is not None and excinfo.errisinstance(reraise):
if reraise is not None and isinstance(excinfo.value, reraise):
raise
result = None
# use the perf counter
@ -325,7 +332,7 @@ def pytest_make_collect_report(collector: Collector) -> CollectReport:
if unittest is not None:
# Type ignored because unittest is loaded dynamically.
skip_exceptions.append(unittest.SkipTest) # type: ignore
if call.excinfo.errisinstance(tuple(skip_exceptions)):
if isinstance(call.excinfo.value, tuple(skip_exceptions)):
outcome = "skipped"
r_ = collector._repr_failure_py(call.excinfo, "line")
assert isinstance(r_, ExceptionChainRepr), repr(r_)

View File

@ -302,9 +302,7 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
if (
unittest
and call.excinfo
and call.excinfo.errisinstance(
unittest.SkipTest # type: ignore[attr-defined] # noqa: F821
)
and isinstance(call.excinfo.value, unittest.SkipTest) # type: ignore[attr-defined]
):
excinfo = call.excinfo
# let's substitute the excinfo with a pytest.skip one