@@ -62,8 +62,7 @@ def _get_version(module: types.ModuleType) -> str:
62
62
def import_optional_dependency (
63
63
name : str ,
64
64
extra : str = "" ,
65
- raise_on_missing : bool = True ,
66
- on_version : str = "raise" ,
65
+ errors : str = "raise" ,
67
66
min_version : Optional [str ] = None ,
68
67
):
69
68
"""
@@ -79,29 +78,30 @@ def import_optional_dependency(
79
78
The module name.
80
79
extra : str
81
80
Additional text to include in the ImportError message.
82
- raise_on_missing : bool, default True
83
- Whether to raise if the optional dependency is not found.
84
- When False and the module is not present, None is returned.
85
- on_version : str {'raise', 'warn'}
86
- What to do when a dependency's version is too old.
81
+ errors : str {'raise', 'warn', 'ignore'}
82
+ What to do when a dependency is not found or its version is too old.
87
83
88
84
* raise : Raise an ImportError
89
- * warn : Warn that the version is too old. Returns None
90
- * ignore: Return the module, even if the version is too old.
85
+ * warn : Only applicable when a module's version is to old.
86
+ Warns that the version is too old and returns None
87
+ * ignore: If the module is not installed, return None, otherwise,
88
+ return the module, even if the version is too old.
91
89
It's expected that users validate the version locally when
92
- using ``on_version ="ignore"`` (see. ``io/html.py``)
90
+ using ``errors ="ignore"`` (see. ``io/html.py``)
93
91
min_version : str, default None
94
92
Specify a minimum version that is different from the global pandas
95
93
minimum version required.
96
94
Returns
97
95
-------
98
96
maybe_module : Optional[ModuleType]
99
97
The imported module, when found and the version is correct.
100
- None is returned when the package is not found and `raise_on_missing `
101
- is False, or when the package's version is too old and `on_version `
98
+ None is returned when the package is not found and `errors `
99
+ is False, or when the package's version is too old and `errors `
102
100
is ``'warn'``.
103
101
"""
104
102
103
+ assert errors in {"warn" , "raise" , "ignore" }
104
+
105
105
package_name = INSTALL_MAPPING .get (name )
106
106
install_name = package_name if package_name is not None else name
107
107
@@ -112,7 +112,7 @@ def import_optional_dependency(
112
112
try :
113
113
module = importlib .import_module (name )
114
114
except ImportError :
115
- if raise_on_missing :
115
+ if errors == "raise" :
116
116
raise ImportError (msg ) from None
117
117
else :
118
118
return None
@@ -128,15 +128,14 @@ def import_optional_dependency(
128
128
if minimum_version :
129
129
version = _get_version (module_to_get )
130
130
if distutils .version .LooseVersion (version ) < minimum_version :
131
- assert on_version in {"warn" , "raise" , "ignore" }
132
131
msg = (
133
132
f"Pandas requires version '{ minimum_version } ' or newer of '{ parent } ' "
134
133
f"(version '{ version } ' currently installed)."
135
134
)
136
- if on_version == "warn" :
135
+ if errors == "warn" :
137
136
warnings .warn (msg , UserWarning )
138
137
return None
139
- elif on_version == "raise" :
138
+ elif errors == "raise" :
140
139
raise ImportError (msg )
141
140
142
141
return module
0 commit comments