-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add support to import optional submodule and specify different min_version than default #38925
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
jreback
merged 2 commits into
pandas-dev:master
from
lithomas1:import-optional-submodule
Jan 4, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import distutils.version | ||
import importlib | ||
import sys | ||
import types | ||
from typing import Optional | ||
import warnings | ||
|
||
# Update install.rst when updating versions! | ||
|
@@ -58,7 +60,11 @@ def _get_version(module: types.ModuleType) -> str: | |
|
||
|
||
def import_optional_dependency( | ||
name: str, extra: str = "", raise_on_missing: bool = True, on_version: str = "raise" | ||
name: str, | ||
extra: str = "", | ||
raise_on_missing: bool = True, | ||
on_version: str = "raise", | ||
min_version: Optional[str] = None, | ||
): | ||
""" | ||
Import an optional dependency. | ||
|
@@ -70,8 +76,7 @@ def import_optional_dependency( | |
Parameters | ||
---------- | ||
name : str | ||
The module name. This should be top-level only, so that the | ||
version may be checked. | ||
The module name. | ||
extra : str | ||
Additional text to include in the ImportError message. | ||
raise_on_missing : bool, default True | ||
|
@@ -85,7 +90,9 @@ def import_optional_dependency( | |
* 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``) | ||
|
||
min_version : str, default None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you create an issue (and PR!) to consolidate |
||
Specify a minimum version that is different from the global pandas | ||
minimum version required. | ||
Returns | ||
------- | ||
maybe_module : Optional[ModuleType] | ||
|
@@ -110,13 +117,20 @@ def import_optional_dependency( | |
else: | ||
return None | ||
|
||
minimum_version = VERSIONS.get(name) | ||
# Handle submodules: if we have submodule, grab parent module from sys.modules | ||
parent = name.split(".")[0] | ||
if parent != name: | ||
install_name = parent | ||
module_to_get = sys.modules[install_name] | ||
else: | ||
module_to_get = module | ||
minimum_version = min_version if min_version is not None else VERSIONS.get(parent) | ||
if minimum_version: | ||
version = _get_version(module) | ||
version = _get_version(module_to_get) | ||
if distutils.version.LooseVersion(version) < minimum_version: | ||
assert on_version in {"warn", "raise", "ignore"} | ||
msg = ( | ||
f"Pandas requires version '{minimum_version}' or newer of '{name}' " | ||
f"Pandas requires version '{minimum_version}' or newer of '{parent}' " | ||
f"(version '{version}' currently installed)." | ||
) | ||
if on_version == "warn": | ||
|
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
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.
i believe we could / should min_version in the codebase, but can be a followon