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:
parent
2ae721cda5
commit
fb2640b82f
|
@ -558,7 +558,10 @@ class ExceptionInfo(Generic[_E]):
|
||||||
def errisinstance(
|
def errisinstance(
|
||||||
self, exc: Union["Type[BaseException]", Tuple["Type[BaseException]", ...]]
|
self, exc: Union["Type[BaseException]", Tuple["Type[BaseException]", ...]]
|
||||||
) -> bool:
|
) -> 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)
|
return isinstance(self.value, exc)
|
||||||
|
|
||||||
def _getreprcrash(self) -> "ReprFileLocation":
|
def _getreprcrash(self) -> "ReprFileLocation":
|
||||||
|
@ -804,7 +807,7 @@ class FormattedExcinfo:
|
||||||
if self.tbfilter:
|
if self.tbfilter:
|
||||||
traceback = traceback.filter()
|
traceback = traceback.filter()
|
||||||
|
|
||||||
if excinfo.errisinstance(RecursionError):
|
if isinstance(excinfo.value, RecursionError):
|
||||||
traceback, extraline = self._truncate_recursive_traceback(traceback)
|
traceback, extraline = self._truncate_recursive_traceback(traceback)
|
||||||
else:
|
else:
|
||||||
extraline = None
|
extraline = None
|
||||||
|
|
|
@ -265,7 +265,7 @@ def wrap_session(
|
||||||
session.exitstatus = exc.returncode
|
session.exitstatus = exc.returncode
|
||||||
sys.stderr.write("{}: {}\n".format(type(exc).__name__, exc))
|
sys.stderr.write("{}: {}\n".format(type(exc).__name__, exc))
|
||||||
else:
|
else:
|
||||||
if excinfo.errisinstance(SystemExit):
|
if isinstance(excinfo.value, SystemExit):
|
||||||
sys.stderr.write("mainloop: caught unexpected SystemExit!\n")
|
sys.stderr.write("mainloop: caught unexpected SystemExit!\n")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -308,7 +308,7 @@ class TestReport(BaseReport):
|
||||||
if not isinstance(excinfo, ExceptionInfo):
|
if not isinstance(excinfo, ExceptionInfo):
|
||||||
outcome = "failed"
|
outcome = "failed"
|
||||||
longrepr = excinfo
|
longrepr = excinfo
|
||||||
elif excinfo.errisinstance(skip.Exception):
|
elif isinstance(excinfo.value, skip.Exception):
|
||||||
outcome = "skipped"
|
outcome = "skipped"
|
||||||
r = excinfo._getreprcrash()
|
r = excinfo._getreprcrash()
|
||||||
longrepr = (str(r.path), r.lineno, r.message)
|
longrepr = (str(r.path), r.lineno, r.message)
|
||||||
|
|
|
@ -215,11 +215,18 @@ def call_and_report(
|
||||||
|
|
||||||
|
|
||||||
def check_interactive_exception(call: "CallInfo", report: BaseReport) -> bool:
|
def check_interactive_exception(call: "CallInfo", report: BaseReport) -> bool:
|
||||||
return call.excinfo is not None and not (
|
"""Check whether the call raised an exception that should be reported as
|
||||||
hasattr(report, "wasxfail")
|
interactive."""
|
||||||
or call.excinfo.errisinstance(Skipped)
|
if call.excinfo is None:
|
||||||
or call.excinfo.errisinstance(bdb.BdbQuit)
|
# 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(
|
def call_runtest_hook(
|
||||||
|
@ -287,7 +294,7 @@ class CallInfo(Generic[_T]):
|
||||||
result = func() # type: Optional[_T]
|
result = func() # type: Optional[_T]
|
||||||
except BaseException:
|
except BaseException:
|
||||||
excinfo = ExceptionInfo.from_current()
|
excinfo = ExceptionInfo.from_current()
|
||||||
if reraise is not None and excinfo.errisinstance(reraise):
|
if reraise is not None and isinstance(excinfo.value, reraise):
|
||||||
raise
|
raise
|
||||||
result = None
|
result = None
|
||||||
# use the perf counter
|
# use the perf counter
|
||||||
|
@ -325,7 +332,7 @@ def pytest_make_collect_report(collector: Collector) -> CollectReport:
|
||||||
if unittest is not None:
|
if unittest is not None:
|
||||||
# Type ignored because unittest is loaded dynamically.
|
# Type ignored because unittest is loaded dynamically.
|
||||||
skip_exceptions.append(unittest.SkipTest) # type: ignore
|
skip_exceptions.append(unittest.SkipTest) # type: ignore
|
||||||
if call.excinfo.errisinstance(tuple(skip_exceptions)):
|
if isinstance(call.excinfo.value, tuple(skip_exceptions)):
|
||||||
outcome = "skipped"
|
outcome = "skipped"
|
||||||
r_ = collector._repr_failure_py(call.excinfo, "line")
|
r_ = collector._repr_failure_py(call.excinfo, "line")
|
||||||
assert isinstance(r_, ExceptionChainRepr), repr(r_)
|
assert isinstance(r_, ExceptionChainRepr), repr(r_)
|
||||||
|
|
|
@ -302,9 +302,7 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
|
||||||
if (
|
if (
|
||||||
unittest
|
unittest
|
||||||
and call.excinfo
|
and call.excinfo
|
||||||
and call.excinfo.errisinstance(
|
and isinstance(call.excinfo.value, unittest.SkipTest) # type: ignore[attr-defined]
|
||||||
unittest.SkipTest # type: ignore[attr-defined] # noqa: F821
|
|
||||||
)
|
|
||||||
):
|
):
|
||||||
excinfo = call.excinfo
|
excinfo = call.excinfo
|
||||||
# let's substitute the excinfo with a pytest.skip one
|
# let's substitute the excinfo with a pytest.skip one
|
||||||
|
|
Loading…
Reference in New Issue