Skip to content

Commit 51826c0

Browse files
committed
TST: refactor testing for hard dependency package
1 parent d65a970 commit 51826c0

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

pandas/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
# Let users know if they're missing any of our hard dependencies
66
_hard_dependencies = ("numpy", "dateutil")
7-
_missing_dependencies = []
87

98
for _dependency in _hard_dependencies:
109
try:
@@ -14,11 +13,7 @@
1413
f"Unable to import required dependency {_dependency} ..."
1514
) from _e
1615

17-
if _missing_dependencies: # pragma: no cover
18-
raise ImportError(
19-
"Unable to import required dependencies:\n" + "\n".join(_missing_dependencies)
20-
)
21-
del _hard_dependencies, _dependency, _missing_dependencies
16+
del _hard_dependencies, _dependency
2217

2318
try:
2419
# numpy compat

pandas/tests/test_downstream.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import array
66
from functools import partial
7+
import importlib
78
import subprocess
89
import sys
910

@@ -186,41 +187,31 @@ def test_yaml_dump(df):
186187
tm.assert_frame_equal(df, loaded2)
187188

188189

189-
@pytest.mark.single_cpu
190-
def test_missing_required_dependency():
191-
# GH 23868
192-
# To ensure proper isolation, we pass these flags
193-
# -S : disable site-packages
194-
# -s : disable user site-packages
195-
# -E : disable PYTHON* env vars, especially PYTHONPATH
196-
# https://github.com/MacPython/pandas-wheels/pull/50
197-
198-
pyexe = sys.executable.replace("\\", "/")
199-
200-
# We skip this test if pandas is installed as a site package. We first
201-
# import the package normally and check the path to the module before
202-
# executing the test which imports pandas with site packages disabled.
203-
call = [pyexe, "-c", "import pandas;print(pandas.__file__)"]
204-
output = subprocess.check_output(call).decode()
205-
if "site-packages" in output:
206-
pytest.skip("pandas installed as site package")
207-
208-
# This test will fail if pandas is installed as a site package. The flags
209-
# prevent pandas being imported and the test will report Failed: DID NOT
210-
# RAISE <class 'subprocess.CalledProcessError'>
211-
call = [pyexe, "-sSE", "-c", "import pandas"]
212-
213-
msg = (
214-
rf"Command '\['{pyexe}', '-sSE', '-c', 'import pandas'\]' "
215-
"returned non-zero exit status 1."
216-
)
190+
@pytest.mark.parametrize("dependency", ["numpy", "dateutil"])
191+
def test_missing_required_dependency(monkeypatch, dependency):
192+
# GH#61030
193+
# test pandas raises appropriate error when a required dependency is missing
194+
real_module = sys.modules.get(dependency)
195+
mock_error = ImportError(f"Mock error for {dependency}")
217196

218-
with pytest.raises(subprocess.CalledProcessError, match=msg) as exc:
219-
subprocess.check_output(call, stderr=subprocess.STDOUT)
197+
def mock_import(name, *args, **kwargs):
198+
if name == dependency:
199+
raise mock_error
200+
return importlib.import_module(name)
201+
202+
try:
203+
monkeypatch.setattr("builtins.__import__", mock_import)
220204

221-
output = exc.value.stdout.decode()
222-
for name in ["numpy", "dateutil"]:
223-
assert name in output
205+
if dependency in sys.modules:
206+
del sys.modules[dependency]
207+
208+
with pytest.raises(ImportError) as excinfo:
209+
importlib.reload(importlib.import_module("pandas"))
210+
211+
assert dependency in str(excinfo.value)
212+
finally:
213+
if real_module is not None:
214+
sys.modules[dependency] = real_module
224215

225216

226217
def test_frame_setitem_dask_array_into_new_col(request):

0 commit comments

Comments
 (0)