Skip to content

Commit 9af6d46

Browse files
authored
Merge pull request #11817 from bluetech/conftesterror-cleanup
config: stop using exception triplets in `ConftestImportError`
2 parents 6e74601 + e1074f9 commit 9af6d46

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

src/_pytest/config/__init__.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from pathlib import Path
1818
from textwrap import dedent
1919
from types import FunctionType
20-
from types import TracebackType
2120
from typing import Any
2221
from typing import Callable
2322
from typing import cast
@@ -112,16 +111,14 @@ class ConftestImportFailure(Exception):
112111
def __init__(
113112
self,
114113
path: Path,
115-
excinfo: Tuple[Type[Exception], Exception, TracebackType],
114+
*,
115+
cause: Exception,
116116
) -> None:
117-
super().__init__(path, excinfo)
118117
self.path = path
119-
self.excinfo = excinfo
118+
self.cause = cause
120119

121120
def __str__(self) -> str:
122-
return "{}: {} (from {})".format(
123-
self.excinfo[0].__name__, self.excinfo[1], self.path
124-
)
121+
return f"{type(self.cause).__name__}: {self.cause} (from {self.path})"
125122

126123

127124
def filter_traceback_for_conftest_import_failure(
@@ -152,7 +149,7 @@ def main(
152149
try:
153150
config = _prepareconfig(args, plugins)
154151
except ConftestImportFailure as e:
155-
exc_info = ExceptionInfo.from_exc_info(e.excinfo)
152+
exc_info = ExceptionInfo.from_exception(e.cause)
156153
tw = TerminalWriter(sys.stderr)
157154
tw.line(f"ImportError while loading conftest '{e.path}'.", red=True)
158155
exc_info.traceback = exc_info.traceback.filter(
@@ -654,8 +651,7 @@ def _importconftest(
654651
mod = import_path(conftestpath, mode=importmode, root=rootpath)
655652
except Exception as e:
656653
assert e.__traceback__ is not None
657-
exc_info = (type(e), e, e.__traceback__)
658-
raise ConftestImportFailure(conftestpath, exc_info) from e
654+
raise ConftestImportFailure(conftestpath, cause=e) from e
659655

660656
self._check_non_top_pytest_plugins(mod, conftestpath)
661657

src/_pytest/debugging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ def _postmortem_traceback(excinfo: ExceptionInfo[BaseException]) -> types.Traceb
377377
elif isinstance(excinfo.value, ConftestImportFailure):
378378
# A config.ConftestImportFailure is not useful for post_mortem.
379379
# Use the underlying exception instead:
380-
return excinfo.value.excinfo[2]
380+
assert excinfo.value.cause.__traceback__ is not None
381+
return excinfo.value.cause.__traceback__
381382
else:
382383
assert excinfo._excinfo is not None
383384
return excinfo._excinfo[2]

src/_pytest/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def _repr_failure_py(
384384
from _pytest.fixtures import FixtureLookupError
385385

386386
if isinstance(excinfo.value, ConftestImportFailure):
387-
excinfo = ExceptionInfo.from_exc_info(excinfo.value.excinfo)
387+
excinfo = ExceptionInfo.from_exception(excinfo.value.cause)
388388
if isinstance(excinfo.value, fail.Exception):
389389
if not excinfo.value.pytrace:
390390
style = "value"

testing/test_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,9 +2108,7 @@ def test_conftest_import_error_repr(tmp_path: Path) -> None:
21082108
try:
21092109
raise RuntimeError("some error")
21102110
except Exception as exc:
2111-
assert exc.__traceback__ is not None
2112-
exc_info = (type(exc), exc, exc.__traceback__)
2113-
raise ConftestImportFailure(path, exc_info) from exc
2111+
raise ConftestImportFailure(path, cause=exc) from exc
21142112

21152113

21162114
def test_strtobool() -> None:

0 commit comments

Comments
 (0)