Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pandas/compat/_optional.py : 41%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import distutils.version
2import importlib
3import types
4import warnings
6# Update install.rst when updating versions!
8VERSIONS = {
9 "bs4": "4.6.0",
10 "bottleneck": "1.2.1",
11 "fastparquet": "0.3.2",
12 "gcsfs": "0.2.2",
13 "lxml.etree": "3.8.0",
14 "matplotlib": "2.2.2",
15 "numexpr": "2.6.2",
16 "odfpy": "1.3.0",
17 "openpyxl": "2.5.7",
18 "pandas_gbq": "0.8.0",
19 "pyarrow": "0.13.0",
20 "pytables": "3.4.2",
21 "pytest": "5.0.1",
22 "pyxlsb": "1.0.6",
23 "s3fs": "0.3.0",
24 "scipy": "0.19.0",
25 "sqlalchemy": "1.1.4",
26 "tables": "3.4.2",
27 "tabulate": "0.8.3",
28 "xarray": "0.8.2",
29 "xlrd": "1.1.0",
30 "xlwt": "1.2.0",
31 "xlsxwriter": "0.9.8",
32 "numba": "0.46.0",
33}
36def _get_version(module: types.ModuleType) -> str:
37 version = getattr(module, "__version__", None)
38 if version is None:
39 # xlrd uses a capitalized attribute name
40 version = getattr(module, "__VERSION__", None)
42 if version is None:
43 raise ImportError(f"Can't determine version for {module.__name__}")
44 return version
47def import_optional_dependency(
48 name: str, extra: str = "", raise_on_missing: bool = True, on_version: str = "raise"
49):
50 """
51 Import an optional dependency.
53 By default, if a dependency is missing an ImportError with a nice
54 message will be raised. If a dependency is present, but too old,
55 we raise.
57 Parameters
58 ----------
59 name : str
60 The module name. This should be top-level only, so that the
61 version may be checked.
62 extra : str
63 Additional text to include in the ImportError message.
64 raise_on_missing : bool, default True
65 Whether to raise if the optional dependency is not found.
66 When False and the module is not present, None is returned.
67 on_version : str {'raise', 'warn'}
68 What to do when a dependency's version is too old.
70 * raise : Raise an ImportError
71 * warn : Warn that the version is too old. Returns None
72 * ignore: Return the module, even if the version is too old.
73 It's expected that users validate the version locally when
74 using ``on_version="ignore"`` (see. ``io/html.py``)
76 Returns
77 -------
78 maybe_module : Optional[ModuleType]
79 The imported module, when found and the version is correct.
80 None is returned when the package is not found and `raise_on_missing`
81 is False, or when the package's version is too old and `on_version`
82 is ``'warn'``.
83 """
84 msg = (
85 f"Missing optional dependency '{name}'. {extra} "
86 f"Use pip or conda to install {name}."
87 )
88 try:
89 module = importlib.import_module(name)
90 except ImportError:
91 if raise_on_missing:
92 raise ImportError(msg) from None
93 else:
94 return None
96 minimum_version = VERSIONS.get(name)
97 if minimum_version:
98 version = _get_version(module)
99 if distutils.version.LooseVersion(version) < minimum_version:
100 assert on_version in {"warn", "raise", "ignore"}
101 msg = (
102 f"Pandas requires version '{minimum_version}' or newer of '{name}' "
103 f"(version '{version}' currently installed)."
104 )
105 if on_version == "warn":
106 warnings.warn(msg, UserWarning)
107 return None
108 elif on_version == "raise":
109 raise ImportError(msg)
111 return module