Skip to content

ERR: include original error message for missing required dependencies #26685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move import test to test_downstream
  • Loading branch information
DanielFEvans committed Jun 6, 2019
commit 0e3038f15cd13955f577b3ef3f9ee25943841ecb
31 changes: 0 additions & 31 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import builtins
from datetime import datetime, timedelta
from importlib import reload
from io import StringIO
import re
import sys
Expand Down Expand Up @@ -1343,32 +1341,3 @@ def test_to_numpy_dtype(as_series):
expected = np.array(['2000-01-01T05', '2001-01-01T05'],
dtype='M8[ns]')
tm.assert_numpy_array_equal(result, expected)


def test_missing_required_dependency(monkeypatch):
original_import = __import__

def mock_import_fail(name, *args, **kwargs):
if name == "numpy":
raise ImportError("cannot import name numpy")
elif name == "pytz":
raise ImportError("cannot import name some_dependency")
elif name == "dateutil":
raise ImportError("cannot import name some_other_dependency")
else:
return original_import(name, *args, **kwargs)

expected_msg = (
"Unable to import required dependencies:"
"\nnumpy: cannot import name numpy"
"\npytz: cannot import name some_dependency"
"\ndateutil: cannot import name some_other_dependency"
)

with monkeypatch.context() as m:
m.setattr(builtins, "__import__", mock_import_fail)
with pytest.raises(ImportError, match=expected_msg):
reload(pd)

# Final reload to ensure normal state of pandas
reload(pd)
30 changes: 30 additions & 0 deletions pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Testing that we work in the downstream packages
"""
import builtins
import importlib
import subprocess
import sys
Expand Down Expand Up @@ -131,3 +132,32 @@ def test_pyarrow(df):
table = pyarrow.Table.from_pandas(df)
result = table.to_pandas()
tm.assert_frame_equal(result, df)


def test_missing_required_dependency(monkeypatch):
original_import = __import__

def mock_import_fail(name, *args, **kwargs):
if name == "numpy":
raise ImportError("cannot import name numpy")
elif name == "pytz":
raise ImportError("cannot import name some_dependency")
elif name == "dateutil":
raise ImportError("cannot import name some_other_dependency")
else:
return original_import(name, *args, **kwargs)

expected_msg = (
"Unable to import required dependencies:"
"\nnumpy: cannot import name numpy"
"\npytz: cannot import name some_dependency"
"\ndateutil: cannot import name some_other_dependency"
)

import pandas as pd

with monkeypatch.context() as m:
m.setattr(builtins, "__import__", mock_import_fail)
with pytest.raises(ImportError, match=expected_msg):
importlib.reload(pd)