-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d674091
REF: Consistent optional dependency handling
TomAugspurger f638d1f
added types
TomAugspurger d4a2986
xlrd
TomAugspurger 5312339
numexpr, bn
TomAugspurger c2b84af
sort
TomAugspurger c16ccd0
Merge remote-tracking branch 'upstream/master' into optional-deps
TomAugspurger eac4382
Merge remote-tracking branch 'upstream/master' into optional-deps
TomAugspurger b65d9af
strict inequality
TomAugspurger bf93ec2
allow ignoring
TomAugspurger d04425d
typing
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import distutils.version | ||
import importlib | ||
import types | ||
from typing import Optional | ||
import warnings | ||
|
||
# Update install.rst when updating versions! | ||
|
||
VERSIONS = { | ||
"bs4": "4.4.1", | ||
"bottleneck": "1.2.1", | ||
"fastparquet": "0.2.1", | ||
"gcsfs": "0.1.0", | ||
"matplotlib": "2.2.2", | ||
"numexpr": "2.6.2", | ||
"openpyxl": "2.4.0", | ||
"pandas_gbq": "0.8.0", | ||
"pyarrow": "0.9.0", | ||
"pytables": "3.4.2", | ||
"s3fs": "0.0.8", | ||
"scipy": "0.19.0", | ||
"sqlalchemy": "1.1.4", | ||
"xarray": "0.8.2", | ||
"xlrd": "1.0.0", | ||
"xlwt": "2.4.0", | ||
} | ||
|
||
message = ( | ||
"Missing optional dependency '{name}'. {extra} " | ||
"Use pip or conda to install {name}." | ||
) | ||
version_message = ( | ||
"Pandas requires version '{minimum_version}' or newer of '{name}' " | ||
"(version '{actual_version}' currently installed)." | ||
) | ||
|
||
|
||
def _get_version(module: types.ModuleType) -> str: | ||
version = getattr(module, '__version__', None) | ||
if version is None: | ||
# xlrd uses a capitalized attribute name | ||
version = getattr(module, '__VERSION__', None) | ||
|
||
if version is None: | ||
raise ImportError( | ||
"Can't determine version for {}".format(module.__name__) | ||
) | ||
return version | ||
|
||
|
||
def import_optional_dependency( | ||
name: str, | ||
extra: str = "", | ||
raise_on_missing: bool = True, | ||
on_version: str = "raise", | ||
) -> Optional[types.ModuleType]: | ||
""" | ||
Import an optional dependency. | ||
|
||
By default, if a dependency is missing an ImportError with a nice | ||
message will be raised. If a dependency is present, but too old, | ||
we raise. | ||
|
||
Parameters | ||
---------- | ||
name : str | ||
The module name. This should be top-level only, so that the | ||
version may be checked. | ||
extra : str | ||
Additional text to include in the ImportError message. | ||
raise_on_missing : bool, default True | ||
Whether to raise if the optional dependency is not found. | ||
When False and the module is not present, None is returned. | ||
on_version : str {'raise', 'warn'} | ||
What to do when a dependency's version is too old. | ||
|
||
* raise : Raise an ImportError | ||
* warn : Warn that the version is too old. Returns None | ||
* ignore: Return the module, even if the version is too old. | ||
It's expected that users validate the version locally when | ||
using ``on_version="ignore"`` (see. ``io/html.py``) | ||
|
||
Returns | ||
------- | ||
maybe_module : Optional[ModuleType] | ||
The imported module, when found and the version is correct. | ||
None is returned when the package is not found and `raise_on_missing` | ||
is False, or when the package's version is too old and `on_version` | ||
is ``'warn'``. | ||
""" | ||
try: | ||
module = importlib.import_module(name) | ||
except ImportError: | ||
if raise_on_missing: | ||
raise ImportError(message.format(name=name, extra=extra)) from None | ||
else: | ||
return None | ||
|
||
minimum_version = VERSIONS.get(name) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if minimum_version: | ||
version = _get_version(module) | ||
if distutils.version.LooseVersion(version) < minimum_version: | ||
assert on_version in {"warn", "raise", "ignore"} | ||
msg = version_message.format( | ||
minimum_version=minimum_version, | ||
name=name, | ||
actual_version=version, | ||
) | ||
if on_version == "warn": | ||
warnings.warn(msg, UserWarning) | ||
return None | ||
elif on_version == "raise": | ||
raise ImportError(msg) | ||
|
||
return module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
from distutils.version import LooseVersion | ||
import warnings | ||
|
||
_NUMEXPR_INSTALLED = False | ||
_MIN_NUMEXPR_VERSION = "2.6.2" | ||
_NUMEXPR_VERSION = None | ||
|
||
try: | ||
import numexpr as ne | ||
ver = LooseVersion(ne.__version__) | ||
_NUMEXPR_INSTALLED = ver >= LooseVersion(_MIN_NUMEXPR_VERSION) | ||
_NUMEXPR_VERSION = ver | ||
|
||
if not _NUMEXPR_INSTALLED: | ||
warnings.warn( | ||
"The installed version of numexpr {ver} is not supported " | ||
"in pandas and will be not be used\nThe minimum supported " | ||
"version is {min_ver}\n".format( | ||
ver=ver, min_ver=_MIN_NUMEXPR_VERSION), UserWarning) | ||
|
||
except ImportError: # pragma: no cover | ||
pass | ||
from pandas.compat._optional import import_optional_dependency | ||
|
||
ne = import_optional_dependency("numexpr", raise_on_missing=False, | ||
on_version="warn") | ||
_NUMEXPR_INSTALLED = ne is not None | ||
if _NUMEXPR_INSTALLED: | ||
_NUMEXPR_VERSION = ne.__version__ | ||
else: | ||
_NUMEXPR_VERSION = None | ||
|
||
__all__ = ['_NUMEXPR_INSTALLED', '_NUMEXPR_VERSION'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)