Skip to content

Commit bed6e3f

Browse files
committed
Remove one of the tracebacks from conftest import failures
This removes the KeyError from the traceback chain when an conftest fails to import: return self._conftestpath2mod[key] E KeyError: WindowsPath('D:/projects/pytest/.tmp/root/foo/conftest.py') During handling of the above exception, another exception occurred: ... raise RuntimeError("some error") E RuntimeError: some error During handling of the above exception, another exception occurred: ... E _pytest.config.ConftestImportFailure: (...) By slightly changing the code, we can remove the first chain, which is often very confusing to users and doesn't help with anything.
1 parent d4dfe86 commit bed6e3f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/_pytest/config/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,10 @@ def _importconftest(self, conftestpath):
511511
# Using Path().resolve() is better than py.path.realpath because
512512
# it resolves to the correct path/drive in case-insensitive file systems (#5792)
513513
key = Path(str(conftestpath)).resolve()
514-
try:
515-
return self._conftestpath2mod[key]
516-
except KeyError:
514+
mod = self._conftestpath2mod.get(key)
515+
if mod is not None:
516+
return mod
517+
else:
517518
pkgpath = conftestpath.pypkgpath()
518519
if pkgpath is None:
519520
_ensure_removed_sysmodule(conftestpath.purebasename)
@@ -525,8 +526,8 @@ def _importconftest(self, conftestpath):
525526
and not self._using_pyargs
526527
):
527528
_fail_on_non_top_pytest_plugins(conftestpath, self._confcutdir)
528-
except Exception:
529-
raise ConftestImportFailure(conftestpath, sys.exc_info())
529+
except Exception as e:
530+
raise ConftestImportFailure(conftestpath, sys.exc_info()) from e
530531

531532
self._conftest_plugins.add(mod)
532533
self._conftestpath2mod[key] = mod

0 commit comments

Comments
 (0)