Skip to content

REF: Consistent optional dependency handling #26802

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 10 commits into from
Jun 12, 2019
14 changes: 9 additions & 5 deletions pandas/compat/_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@


def _get_version(module: types.ModuleType) -> str:
try:
version = module.__version__
except AttributeError:
version = getattr(module, '__version__', None)
if version is None:
# xlrd uses a capitalized attribute name
version = module.__VERSION__
version = getattr(module, '__VERSION__', None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we not encode the 'version' (or 'VERSION') with the list of deps directly? (or have a way to override); in fact we already do this in pandas/util/_print_versions. so actually I would update that to use this list of deps (and also to add the version accessor)


if version is None:
raise ImportError(
"Can't determine version for {}".format(module.__name__)
)
return version


Expand Down Expand Up @@ -90,7 +94,7 @@ def import_optional_dependency(
if raise_on_missing:
raise ImportError(message.format(name=name, extra=extra)) from None
else:
return
return None

minimum_version = VERSIONS.get(name)
if minimum_version:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_optional_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ def test_bad_version():
module.__version__ = "1.0.0" # exact match is OK
result = import_optional_dependency("fakemodule")
assert result is module


def test_no_version_raises():
name = 'fakemodule'
module = types.ModuleType(name)
sys.modules[name] = module
VERSIONS[name] = '1.0.0'

with pytest.raises(ImportError, match="Can't determine .* fakemodule"):
import_optional_dependency(name)