| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import importlib |
| 4 |
import sys |
| 5 |
import types |
| 6 |
import warnings |
| 7 |
from typing import Literal |
| 8 |
|
| 9 |
if sys.version_info >= (3, 12): |
| 10 |
import importlib.metadata as importlib_metadata |
| 11 |
else: |
| 12 |
import importlib_metadata |
| 13 |
from packaging.version import Version |
| 14 |
|
| 15 |
PY_VERSION = Version(".".join(map(str, sys.version_info[:3]))) |
| 16 |
|
| 17 |
EMSCRIPTEN = sys.platform == "emscripten" |
| 18 |
|
| 19 |
LINUX = sys.platform == "linux" |
| 20 |
MACOS = sys.platform == "darwin" |
| 21 |
WINDOWS = sys.platform == "win32" |
| 22 |
|
| 23 |
|
| 24 |
def entry_points(group=None): |
| 25 |
warnings.warn( |
| 26 |
"`dask._compatibility.entry_points` has been replaced by `importlib_metadata.entry_points` and will be removed " |
| 27 |
"in a future version. Please use `importlib_metadata.entry_points` instead.", |
| 28 |
DeprecationWarning, |
| 29 |
stacklevel=2, |
| 30 |
) |
| 31 |
return importlib_metadata.entry_points(group=group) |
| 32 |
|
| 33 |
|
| 34 |
VERSIONS = { |
| 35 |
"numpy": "1.21.0", |
| 36 |
"pandas": "2.0.0", |
| 37 |
"bokeh": "3.1.0", |
| 38 |
"jinja2": "2.10.3", |
| 39 |
"pyarrow": "7.0.0", |
| 40 |
"lz4": "4.3.2", |
| 41 |
} |
| 42 |
|
| 43 |
# A mapping from import name to package name (on PyPI) for packages where |
| 44 |
# these two names are different. |
| 45 |
|
| 46 |
INSTALL_MAPPING = { |
| 47 |
"sqlalchemy": "SQLAlchemy", |
| 48 |
"tables": "pytables", |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def get_version(module: types.ModuleType) -> str: |
| 53 |
version = getattr(module, "__version__", None) |
| 54 |
|
| 55 |
if version is None: |
| 56 |
raise ImportError(f"Can't determine version for {module.__name__}") |
| 57 |
if module.__name__ == "psycopg2": |
| 58 |
# psycopg2 appends " (dt dec pq3 ext lo64)" to it's version |
| 59 |
version = version.split()[0] |
| 60 |
return version |
| 61 |
|
| 62 |
|
| 63 |
def import_optional_dependency( |
| 64 |
name: str, |
| 65 |
extra: str = "", |
| 66 |
min_version: str | None = None, |
| 67 |
*, |
| 68 |
errors: Literal["raise", "warn", "ignore"] = "raise", |
| 69 |
) -> types.ModuleType | None: |
| 70 |
""" |
| 71 |
Import an optional dependency. |
| 72 |
|
| 73 |
By default, if a dependency is missing an ImportError with a nice |
| 74 |
message will be raised. If a dependency is present, but too old, |
| 75 |
we raise. |
| 76 |
|
| 77 |
Parameters |
| 78 |
---------- |
| 79 |
name : str |
| 80 |
The module name. |
| 81 |
extra : str |
| 82 |
Additional text to include in the ImportError message. |
| 83 |
errors : str {'raise', 'warn', 'ignore'} |
| 84 |
What to do when a dependency is not found or its version is too old. |
| 85 |
|
| 86 |
* raise : Raise an ImportError |
| 87 |
* warn : Only applicable when a module's version is to old. |
| 88 |
Warns that the version is too old and returns None |
| 89 |
* ignore: If the module is not installed, return None, otherwise, |
| 90 |
return the module, even if the version is too old. |
| 91 |
It's expected that users validate the version locally when |
| 92 |
using ``errors="ignore"`` (see. ``io/html.py``) |
| 93 |
min_version : str, default None |
| 94 |
Specify a minimum version that is different from the global pandas |
| 95 |
minimum version required. |
| 96 |
Returns |
| 97 |
------- |
| 98 |
maybe_module : Optional[ModuleType] |
| 99 |
The imported module, when found and the version is correct. |
| 100 |
None is returned when the package is not found and `errors` |
| 101 |
is False, or when the package's version is too old and `errors` |
| 102 |
is ``'warn'`` or ``'ignore'``. |
| 103 |
""" |
| 104 |
assert errors in {"warn", "raise", "ignore"} |
| 105 |
|
| 106 |
package_name = INSTALL_MAPPING.get(name) |
| 107 |
install_name = package_name if package_name is not None else name |
| 108 |
|
| 109 |
msg = ( |
| 110 |
f"Missing optional dependency '{install_name}'. {extra} " |
| 111 |
f"Use pip or conda to install {install_name}." |
| 112 |
) |
| 113 |
try: |
| 114 |
module = importlib.import_module(name) |
| 115 |
except ImportError as err: |
| 116 |
if errors == "raise": |
| 117 |
raise ImportError(msg) from err |
| 118 |
return None |
| 119 |
|
| 120 |
# Handle submodules: if we have submodule, grab parent module from sys.modules |
| 121 |
parent = name.split(".")[0] |
| 122 |
if parent != name: |
| 123 |
install_name = parent |
| 124 |
module_to_get = sys.modules[install_name] |
| 125 |
else: |
| 126 |
module_to_get = module |
| 127 |
minimum_version = min_version if min_version is not None else VERSIONS.get(parent) |
| 128 |
if minimum_version: |
| 129 |
version = get_version(module_to_get) |
| 130 |
if version and Version(version) < Version(minimum_version): |
| 131 |
msg = ( |
| 132 |
f"Dask requires version '{minimum_version}' or newer of '{parent}' " |
| 133 |
f"(version '{version}' currently installed)." |
| 134 |
) |
| 135 |
if errors == "warn": |
| 136 |
warnings.warn(msg, UserWarning) |
| 137 |
return None |
| 138 |
elif errors == "raise": |
| 139 |
raise ImportError(msg) |
| 140 |
else: |
| 141 |
return None |
| 142 |
|
| 143 |
return module |