Merge branch 'importorskip-ModuleNotFoundError'

This commit is contained in:
shekhuverma 2024-04-20 12:43:39 +05:30
commit 3ca5036cd0
2 changed files with 18 additions and 8 deletions

View File

@ -232,20 +232,25 @@ def importorskip(
warnings.simplefilter("ignore")
if exc_type is None:
exc_type = ModuleNotFoundError
else:
exc_type = ImportError
warnings.warn(
PytestDeprecationWarning(
"The Default behaviour will change to ImportError in future",
)
)
warn_on_import_error = True
else:
warn_on_import_error = False
try:
__import__(modname)
except exc_type as exc:
if reason is None:
reason = f"could not import {modname!r}: {exc}"
if warn_on_import_error and type(exc) is ImportError:
warnings.warn(
PytestDeprecationWarning(
f"""pytest.importorskip() caught {exc},but this will change in a future pytest release
to only capture ModuleNotFoundError exceptions by default.\nTo overwrite the future
behavior and silence this warning, pass exc_type=ImportError explicitly."""
)
)
raise Skipped(reason, allow_module_level=True) from None
mod = sys.modules[modname]

View File

@ -774,7 +774,12 @@ def test_importorskip_importError_warning(pytester: Pytester) -> None:
with pytest.raises(pytest.skip.Exception):
with pytest.warns(PytestDeprecationWarning):
pytest.importorskip(fn.stem, exc_type=ImportError)
pytest.importorskip(fn.stem)
def test_importorskip_ModuleNotFoundError() -> None:
with pytest.raises(pytest.skip.Exception):
pytest.importorskip("abcdefgh")
def test_importorskip_dev_module(monkeypatch) -> None: