scikit-base 0.4.6__py3-none-any.whl → 0.5.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docs/source/conf.py +299 -299
- {scikit_base-0.4.6.dist-info → scikit_base-0.5.1.dist-info}/LICENSE +29 -29
- {scikit_base-0.4.6.dist-info → scikit_base-0.5.1.dist-info}/METADATA +160 -159
- scikit_base-0.5.1.dist-info/RECORD +58 -0
- {scikit_base-0.4.6.dist-info → scikit_base-0.5.1.dist-info}/WHEEL +1 -1
- scikit_base-0.5.1.dist-info/top_level.txt +5 -0
- {scikit_base-0.4.6.dist-info → scikit_base-0.5.1.dist-info}/zip-safe +1 -1
- skbase/__init__.py +14 -14
- skbase/_exceptions.py +31 -31
- skbase/_nopytest_tests.py +35 -35
- skbase/base/__init__.py +20 -20
- skbase/base/_base.py +1249 -1249
- skbase/base/_meta.py +883 -871
- skbase/base/_pretty_printing/__init__.py +11 -11
- skbase/base/_pretty_printing/_object_html_repr.py +392 -392
- skbase/base/_pretty_printing/_pprint.py +412 -412
- skbase/base/_tagmanager.py +217 -217
- skbase/lookup/__init__.py +31 -31
- skbase/lookup/_lookup.py +1009 -1009
- skbase/lookup/tests/__init__.py +2 -2
- skbase/lookup/tests/test_lookup.py +991 -991
- skbase/testing/__init__.py +12 -12
- skbase/testing/test_all_objects.py +852 -856
- skbase/testing/utils/__init__.py +5 -5
- skbase/testing/utils/_conditional_fixtures.py +209 -209
- skbase/testing/utils/_dependencies.py +15 -15
- skbase/testing/utils/deep_equals.py +15 -15
- skbase/testing/utils/inspect.py +30 -30
- skbase/testing/utils/tests/__init__.py +2 -2
- skbase/testing/utils/tests/test_check_dependencies.py +49 -49
- skbase/testing/utils/tests/test_deep_equals.py +66 -66
- skbase/tests/__init__.py +2 -2
- skbase/tests/conftest.py +273 -273
- skbase/tests/mock_package/__init__.py +5 -5
- skbase/tests/mock_package/test_mock_package.py +74 -74
- skbase/tests/test_base.py +1202 -1202
- skbase/tests/test_baseestimator.py +130 -130
- skbase/tests/test_exceptions.py +23 -23
- skbase/tests/test_meta.py +170 -131
- skbase/utils/__init__.py +21 -21
- skbase/utils/_check.py +53 -53
- skbase/utils/_iter.py +238 -238
- skbase/utils/_nested_iter.py +180 -180
- skbase/utils/_utils.py +91 -91
- skbase/utils/deep_equals.py +358 -358
- skbase/utils/dependencies/__init__.py +11 -11
- skbase/utils/dependencies/_dependencies.py +253 -253
- skbase/utils/tests/__init__.py +4 -4
- skbase/utils/tests/test_check.py +24 -24
- skbase/utils/tests/test_iter.py +127 -127
- skbase/utils/tests/test_nested_iter.py +84 -84
- skbase/utils/tests/test_utils.py +37 -37
- skbase/validate/__init__.py +22 -22
- skbase/validate/_named_objects.py +403 -403
- skbase/validate/_types.py +345 -345
- skbase/validate/tests/__init__.py +2 -2
- skbase/validate/tests/test_iterable_named_objects.py +200 -200
- skbase/validate/tests/test_type_validations.py +370 -370
- scikit_base-0.4.6.dist-info/RECORD +0 -58
- scikit_base-0.4.6.dist-info/top_level.txt +0 -2
@@ -1,11 +1,11 @@
|
|
1
|
-
#!/usr/bin/env python3 -u
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
# copyright: skbase developers, BSD-3-Clause License (see LICENSE file)
|
4
|
-
"""Utility functionality used through `skbase`."""
|
5
|
-
|
6
|
-
from skbase.utils.dependencies._dependencies import (
|
7
|
-
_check_python_version,
|
8
|
-
_check_soft_dependencies,
|
9
|
-
)
|
10
|
-
|
11
|
-
__all__ = ["_check_python_version", "_check_soft_dependencies"]
|
1
|
+
#!/usr/bin/env python3 -u
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# copyright: skbase developers, BSD-3-Clause License (see LICENSE file)
|
4
|
+
"""Utility functionality used through `skbase`."""
|
5
|
+
|
6
|
+
from skbase.utils.dependencies._dependencies import (
|
7
|
+
_check_python_version,
|
8
|
+
_check_soft_dependencies,
|
9
|
+
)
|
10
|
+
|
11
|
+
__all__ = ["_check_python_version", "_check_soft_dependencies"]
|
@@ -1,253 +1,253 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
"""Utility to check soft dependency imports, and raise warnings or errors."""
|
3
|
-
import io
|
4
|
-
import sys
|
5
|
-
import warnings
|
6
|
-
from importlib import import_module
|
7
|
-
from inspect import isclass
|
8
|
-
from typing import List
|
9
|
-
|
10
|
-
from packaging.requirements import InvalidRequirement, Requirement
|
11
|
-
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
12
|
-
|
13
|
-
__author__: List[str] = ["fkiraly", "mloning"]
|
14
|
-
|
15
|
-
|
16
|
-
def _check_soft_dependencies(
|
17
|
-
*packages,
|
18
|
-
package_import_alias=None,
|
19
|
-
severity="error",
|
20
|
-
obj=None,
|
21
|
-
suppress_import_stdout=False,
|
22
|
-
):
|
23
|
-
"""Check if required soft dependencies are installed and raise error or warning.
|
24
|
-
|
25
|
-
Parameters
|
26
|
-
----------
|
27
|
-
packages : str or list/tuple of str, or length-1-tuple containing list/tuple of str
|
28
|
-
str should be package names and/or package version specifications to check.
|
29
|
-
Each str must be a PEP 440 compatibe specifier string, for a single package.
|
30
|
-
For instance, the PEP 440 compatible package name such as "pandas";
|
31
|
-
or a package requirement specifier string such as "pandas>1.2.3".
|
32
|
-
arg can be str, kwargs tuple, or tuple/list of str, following calls are valid:
|
33
|
-
`_check_soft_dependencies("package1")`
|
34
|
-
`_check_soft_dependencies("package1", "package2")`
|
35
|
-
`_check_soft_dependencies(("package1", "package2"))`
|
36
|
-
`_check_soft_dependencies(["package1", "package2"])`
|
37
|
-
package_import_alias : dict with str keys and values, optional, default=empty
|
38
|
-
key-value pairs are package name, import name
|
39
|
-
import name is str used in python import, i.e., from import_name import ...
|
40
|
-
should be provided if import name differs from package name
|
41
|
-
severity : str, "error" (default), "warning", "none"
|
42
|
-
behaviour for raising errors or warnings
|
43
|
-
"error" - raises a `ModuleNotFoundException` if one of packages is not installed
|
44
|
-
"warning" - raises a warning if one of packages is not installed
|
45
|
-
function returns False if one of packages is not installed, otherwise True
|
46
|
-
"none" - does not raise exception or warning
|
47
|
-
function returns False if one of packages is not installed, otherwise True
|
48
|
-
obj : python class, object, str, or None, default=None
|
49
|
-
if self is passed here when _check_soft_dependencies is called within __init__,
|
50
|
-
or a class is passed when it is called at the start of a single-class module,
|
51
|
-
the error message is more informative and will refer to the class/object;
|
52
|
-
if str is passed, will be used as name of the class/object or module
|
53
|
-
suppress_import_stdout : bool, optional. Default=False
|
54
|
-
whether to suppress stdout printout upon import.
|
55
|
-
|
56
|
-
Raises
|
57
|
-
------
|
58
|
-
ModuleNotFoundError
|
59
|
-
error with informative message, asking to install required soft dependencies
|
60
|
-
|
61
|
-
Returns
|
62
|
-
-------
|
63
|
-
boolean - whether all packages are installed, only if no exception is raised
|
64
|
-
"""
|
65
|
-
if len(packages) == 1 and isinstance(packages[0], (tuple, list)):
|
66
|
-
packages = packages[0]
|
67
|
-
if not all(isinstance(x, str) for x in packages):
|
68
|
-
raise TypeError("packages must be str or tuple of str")
|
69
|
-
|
70
|
-
if package_import_alias is None:
|
71
|
-
package_import_alias = {}
|
72
|
-
msg = "package_import_alias must be a dict with str keys and values"
|
73
|
-
if not isinstance(package_import_alias, dict):
|
74
|
-
raise TypeError(msg)
|
75
|
-
if not all(isinstance(x, str) for x in package_import_alias.keys()):
|
76
|
-
raise TypeError(msg)
|
77
|
-
if not all(isinstance(x, str) for x in package_import_alias.values()):
|
78
|
-
raise TypeError(msg)
|
79
|
-
|
80
|
-
if obj is None:
|
81
|
-
class_name = "This functionality"
|
82
|
-
elif not isclass(obj):
|
83
|
-
class_name = type(obj).__name__
|
84
|
-
elif isclass(obj):
|
85
|
-
class_name = obj.__name__
|
86
|
-
elif isinstance(obj, str):
|
87
|
-
class_name = obj
|
88
|
-
else:
|
89
|
-
raise TypeError("obj must be a class, an object, a str, or None")
|
90
|
-
|
91
|
-
for package in packages:
|
92
|
-
try:
|
93
|
-
req = Requirement(package)
|
94
|
-
except InvalidRequirement:
|
95
|
-
msg_version = (
|
96
|
-
f"wrong format for package requirement string, "
|
97
|
-
f'must be PEP 440 compatible requirement string, e.g., "pandas"'
|
98
|
-
f' or "pandas>1.1", but found {package!r}'
|
99
|
-
)
|
100
|
-
raise InvalidRequirement(msg_version) from None
|
101
|
-
|
102
|
-
package_name = req.name
|
103
|
-
package_version_req = req.specifier
|
104
|
-
|
105
|
-
# determine the package import
|
106
|
-
if package_name in package_import_alias.keys():
|
107
|
-
package_import_name = package_import_alias[package_name]
|
108
|
-
else:
|
109
|
-
package_import_name = package_name
|
110
|
-
# attempt import - if not possible, we know we need to raise warning/exception
|
111
|
-
try:
|
112
|
-
if suppress_import_stdout:
|
113
|
-
# setup text trap, import, then restore
|
114
|
-
sys.stdout = io.StringIO()
|
115
|
-
pkg_ref = import_module(package_import_name)
|
116
|
-
sys.stdout = sys.__stdout__
|
117
|
-
else:
|
118
|
-
pkg_ref = import_module(package_import_name)
|
119
|
-
# if package cannot be imported, make the user aware of installation requirement
|
120
|
-
except ModuleNotFoundError as e:
|
121
|
-
msg = (
|
122
|
-
f"{e}. "
|
123
|
-
f"{class_name} requires package {package!r} to be present "
|
124
|
-
f"in the python environment, but {package!r} was not found. "
|
125
|
-
)
|
126
|
-
if obj is not None:
|
127
|
-
msg = msg + (
|
128
|
-
f"{package!r} is a dependency of {class_name} and required "
|
129
|
-
f"to construct it. "
|
130
|
-
)
|
131
|
-
msg = msg + (
|
132
|
-
f"Please run: `pip install {package}` to "
|
133
|
-
f"install the {package} package. "
|
134
|
-
)
|
135
|
-
|
136
|
-
if severity == "error":
|
137
|
-
raise ModuleNotFoundError(msg) from e
|
138
|
-
elif severity == "warning":
|
139
|
-
warnings.warn(msg, stacklevel=2)
|
140
|
-
return False
|
141
|
-
elif severity == "none":
|
142
|
-
return False
|
143
|
-
else:
|
144
|
-
raise RuntimeError(
|
145
|
-
"Error in calling _check_soft_dependencies, severity "
|
146
|
-
'argument must be "error", "warning", or "none",'
|
147
|
-
f"found {severity!r}."
|
148
|
-
) from e
|
149
|
-
|
150
|
-
# now we check compatibility with the version specifier if non-empty
|
151
|
-
if package_version_req != SpecifierSet(""):
|
152
|
-
pkg_env_version = pkg_ref.__version__
|
153
|
-
|
154
|
-
msg = (
|
155
|
-
f"{class_name} requires package {package!r} to be present "
|
156
|
-
f"in the python environment, with version {package_version_req}, "
|
157
|
-
f"but incompatible version {pkg_env_version} was found. "
|
158
|
-
)
|
159
|
-
if obj is not None:
|
160
|
-
msg = msg + (
|
161
|
-
f"{package!r}, with version {package_version_req},"
|
162
|
-
f"is a dependency of {class_name} and required to construct it. "
|
163
|
-
)
|
164
|
-
|
165
|
-
# raise error/warning or return False if version is incompatible
|
166
|
-
if pkg_env_version not in package_version_req:
|
167
|
-
if severity == "error":
|
168
|
-
raise ModuleNotFoundError(msg)
|
169
|
-
elif severity == "warning":
|
170
|
-
warnings.warn(msg, stacklevel=2)
|
171
|
-
elif severity == "none":
|
172
|
-
return False
|
173
|
-
else:
|
174
|
-
raise RuntimeError(
|
175
|
-
"Error in calling _check_soft_dependencies, severity argument"
|
176
|
-
f' must be "error", "warning", or "none", found {severity!r}.'
|
177
|
-
)
|
178
|
-
|
179
|
-
# if package can be imported and no version issue was caught for any string,
|
180
|
-
# then obj is compatible with the requirements and we should return True
|
181
|
-
return True
|
182
|
-
|
183
|
-
|
184
|
-
def _check_python_version(obj, package=None, msg=None, severity="error"):
|
185
|
-
"""Check if system python version is compatible with requirements of obj.
|
186
|
-
|
187
|
-
Parameters
|
188
|
-
----------
|
189
|
-
obj : BaseObject descendant
|
190
|
-
used to check python version
|
191
|
-
package : str, default = None
|
192
|
-
if given, will be used in error message as package name
|
193
|
-
msg : str, optional, default = default message (msg below)
|
194
|
-
error message to be returned in the `ModuleNotFoundError`, overrides default
|
195
|
-
severity : str, "error" (default), "warning", or "none"
|
196
|
-
whether the check should raise an error, a warning, or nothing
|
197
|
-
|
198
|
-
Returns
|
199
|
-
-------
|
200
|
-
compatible : bool, whether obj is compatible with system python version
|
201
|
-
check is using the python_version tag of obj
|
202
|
-
|
203
|
-
Raises
|
204
|
-
------
|
205
|
-
ModuleNotFoundError
|
206
|
-
User friendly error if obj has python_version tag that is
|
207
|
-
incompatible with the system python version. If package is given,
|
208
|
-
error message gives package as the reason for incompatibility.
|
209
|
-
"""
|
210
|
-
est_specifier_tag = obj.get_class_tag("python_version", tag_value_default="None")
|
211
|
-
if est_specifier_tag in ["None", None]:
|
212
|
-
return True
|
213
|
-
|
214
|
-
try:
|
215
|
-
est_specifier = SpecifierSet(est_specifier_tag)
|
216
|
-
except InvalidSpecifier:
|
217
|
-
msg_version = (
|
218
|
-
f"wrong format for python_version tag, "
|
219
|
-
f'must be PEP 440 compatible specifier string, e.g., "<3.9, >= 3.6.3",'
|
220
|
-
f" but found {est_specifier_tag!r}"
|
221
|
-
)
|
222
|
-
raise InvalidSpecifier(msg_version) from None
|
223
|
-
|
224
|
-
# python sys version, e.g., "3.8.12"
|
225
|
-
sys_version = sys.version.split(" ")[0]
|
226
|
-
|
227
|
-
if sys_version in est_specifier:
|
228
|
-
return True
|
229
|
-
# now we know that est_version is not compatible with sys_version
|
230
|
-
|
231
|
-
if not isinstance(msg, str):
|
232
|
-
msg = (
|
233
|
-
f"{type(obj).__name__} requires python version to be {est_specifier},"
|
234
|
-
f" but system python version is {sys.version}."
|
235
|
-
)
|
236
|
-
|
237
|
-
if package is not None:
|
238
|
-
msg += (
|
239
|
-
f" This is due to python version requirements of the {package} package."
|
240
|
-
)
|
241
|
-
|
242
|
-
if severity == "error":
|
243
|
-
raise ModuleNotFoundError(msg)
|
244
|
-
elif severity == "warning":
|
245
|
-
warnings.warn(msg, stacklevel=2)
|
246
|
-
elif severity == "none":
|
247
|
-
return False
|
248
|
-
else:
|
249
|
-
raise RuntimeError(
|
250
|
-
"Error in calling _check_python_version, severity "
|
251
|
-
f'argument must be "error", "warning", or "none", found {severity!r}.'
|
252
|
-
)
|
253
|
-
return True
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
"""Utility to check soft dependency imports, and raise warnings or errors."""
|
3
|
+
import io
|
4
|
+
import sys
|
5
|
+
import warnings
|
6
|
+
from importlib import import_module
|
7
|
+
from inspect import isclass
|
8
|
+
from typing import List
|
9
|
+
|
10
|
+
from packaging.requirements import InvalidRequirement, Requirement
|
11
|
+
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
12
|
+
|
13
|
+
__author__: List[str] = ["fkiraly", "mloning"]
|
14
|
+
|
15
|
+
|
16
|
+
def _check_soft_dependencies(
|
17
|
+
*packages,
|
18
|
+
package_import_alias=None,
|
19
|
+
severity="error",
|
20
|
+
obj=None,
|
21
|
+
suppress_import_stdout=False,
|
22
|
+
):
|
23
|
+
"""Check if required soft dependencies are installed and raise error or warning.
|
24
|
+
|
25
|
+
Parameters
|
26
|
+
----------
|
27
|
+
packages : str or list/tuple of str, or length-1-tuple containing list/tuple of str
|
28
|
+
str should be package names and/or package version specifications to check.
|
29
|
+
Each str must be a PEP 440 compatibe specifier string, for a single package.
|
30
|
+
For instance, the PEP 440 compatible package name such as "pandas";
|
31
|
+
or a package requirement specifier string such as "pandas>1.2.3".
|
32
|
+
arg can be str, kwargs tuple, or tuple/list of str, following calls are valid:
|
33
|
+
`_check_soft_dependencies("package1")`
|
34
|
+
`_check_soft_dependencies("package1", "package2")`
|
35
|
+
`_check_soft_dependencies(("package1", "package2"))`
|
36
|
+
`_check_soft_dependencies(["package1", "package2"])`
|
37
|
+
package_import_alias : dict with str keys and values, optional, default=empty
|
38
|
+
key-value pairs are package name, import name
|
39
|
+
import name is str used in python import, i.e., from import_name import ...
|
40
|
+
should be provided if import name differs from package name
|
41
|
+
severity : str, "error" (default), "warning", "none"
|
42
|
+
behaviour for raising errors or warnings
|
43
|
+
"error" - raises a `ModuleNotFoundException` if one of packages is not installed
|
44
|
+
"warning" - raises a warning if one of packages is not installed
|
45
|
+
function returns False if one of packages is not installed, otherwise True
|
46
|
+
"none" - does not raise exception or warning
|
47
|
+
function returns False if one of packages is not installed, otherwise True
|
48
|
+
obj : python class, object, str, or None, default=None
|
49
|
+
if self is passed here when _check_soft_dependencies is called within __init__,
|
50
|
+
or a class is passed when it is called at the start of a single-class module,
|
51
|
+
the error message is more informative and will refer to the class/object;
|
52
|
+
if str is passed, will be used as name of the class/object or module
|
53
|
+
suppress_import_stdout : bool, optional. Default=False
|
54
|
+
whether to suppress stdout printout upon import.
|
55
|
+
|
56
|
+
Raises
|
57
|
+
------
|
58
|
+
ModuleNotFoundError
|
59
|
+
error with informative message, asking to install required soft dependencies
|
60
|
+
|
61
|
+
Returns
|
62
|
+
-------
|
63
|
+
boolean - whether all packages are installed, only if no exception is raised
|
64
|
+
"""
|
65
|
+
if len(packages) == 1 and isinstance(packages[0], (tuple, list)):
|
66
|
+
packages = packages[0]
|
67
|
+
if not all(isinstance(x, str) for x in packages):
|
68
|
+
raise TypeError("packages must be str or tuple of str")
|
69
|
+
|
70
|
+
if package_import_alias is None:
|
71
|
+
package_import_alias = {}
|
72
|
+
msg = "package_import_alias must be a dict with str keys and values"
|
73
|
+
if not isinstance(package_import_alias, dict):
|
74
|
+
raise TypeError(msg)
|
75
|
+
if not all(isinstance(x, str) for x in package_import_alias.keys()):
|
76
|
+
raise TypeError(msg)
|
77
|
+
if not all(isinstance(x, str) for x in package_import_alias.values()):
|
78
|
+
raise TypeError(msg)
|
79
|
+
|
80
|
+
if obj is None:
|
81
|
+
class_name = "This functionality"
|
82
|
+
elif not isclass(obj):
|
83
|
+
class_name = type(obj).__name__
|
84
|
+
elif isclass(obj):
|
85
|
+
class_name = obj.__name__
|
86
|
+
elif isinstance(obj, str):
|
87
|
+
class_name = obj
|
88
|
+
else:
|
89
|
+
raise TypeError("obj must be a class, an object, a str, or None")
|
90
|
+
|
91
|
+
for package in packages:
|
92
|
+
try:
|
93
|
+
req = Requirement(package)
|
94
|
+
except InvalidRequirement:
|
95
|
+
msg_version = (
|
96
|
+
f"wrong format for package requirement string, "
|
97
|
+
f'must be PEP 440 compatible requirement string, e.g., "pandas"'
|
98
|
+
f' or "pandas>1.1", but found {package!r}'
|
99
|
+
)
|
100
|
+
raise InvalidRequirement(msg_version) from None
|
101
|
+
|
102
|
+
package_name = req.name
|
103
|
+
package_version_req = req.specifier
|
104
|
+
|
105
|
+
# determine the package import
|
106
|
+
if package_name in package_import_alias.keys():
|
107
|
+
package_import_name = package_import_alias[package_name]
|
108
|
+
else:
|
109
|
+
package_import_name = package_name
|
110
|
+
# attempt import - if not possible, we know we need to raise warning/exception
|
111
|
+
try:
|
112
|
+
if suppress_import_stdout:
|
113
|
+
# setup text trap, import, then restore
|
114
|
+
sys.stdout = io.StringIO()
|
115
|
+
pkg_ref = import_module(package_import_name)
|
116
|
+
sys.stdout = sys.__stdout__
|
117
|
+
else:
|
118
|
+
pkg_ref = import_module(package_import_name)
|
119
|
+
# if package cannot be imported, make the user aware of installation requirement
|
120
|
+
except ModuleNotFoundError as e:
|
121
|
+
msg = (
|
122
|
+
f"{e}. "
|
123
|
+
f"{class_name} requires package {package!r} to be present "
|
124
|
+
f"in the python environment, but {package!r} was not found. "
|
125
|
+
)
|
126
|
+
if obj is not None:
|
127
|
+
msg = msg + (
|
128
|
+
f"{package!r} is a dependency of {class_name} and required "
|
129
|
+
f"to construct it. "
|
130
|
+
)
|
131
|
+
msg = msg + (
|
132
|
+
f"Please run: `pip install {package}` to "
|
133
|
+
f"install the {package} package. "
|
134
|
+
)
|
135
|
+
|
136
|
+
if severity == "error":
|
137
|
+
raise ModuleNotFoundError(msg) from e
|
138
|
+
elif severity == "warning":
|
139
|
+
warnings.warn(msg, stacklevel=2)
|
140
|
+
return False
|
141
|
+
elif severity == "none":
|
142
|
+
return False
|
143
|
+
else:
|
144
|
+
raise RuntimeError(
|
145
|
+
"Error in calling _check_soft_dependencies, severity "
|
146
|
+
'argument must be "error", "warning", or "none",'
|
147
|
+
f"found {severity!r}."
|
148
|
+
) from e
|
149
|
+
|
150
|
+
# now we check compatibility with the version specifier if non-empty
|
151
|
+
if package_version_req != SpecifierSet(""):
|
152
|
+
pkg_env_version = pkg_ref.__version__
|
153
|
+
|
154
|
+
msg = (
|
155
|
+
f"{class_name} requires package {package!r} to be present "
|
156
|
+
f"in the python environment, with version {package_version_req}, "
|
157
|
+
f"but incompatible version {pkg_env_version} was found. "
|
158
|
+
)
|
159
|
+
if obj is not None:
|
160
|
+
msg = msg + (
|
161
|
+
f"{package!r}, with version {package_version_req},"
|
162
|
+
f"is a dependency of {class_name} and required to construct it. "
|
163
|
+
)
|
164
|
+
|
165
|
+
# raise error/warning or return False if version is incompatible
|
166
|
+
if pkg_env_version not in package_version_req:
|
167
|
+
if severity == "error":
|
168
|
+
raise ModuleNotFoundError(msg)
|
169
|
+
elif severity == "warning":
|
170
|
+
warnings.warn(msg, stacklevel=2)
|
171
|
+
elif severity == "none":
|
172
|
+
return False
|
173
|
+
else:
|
174
|
+
raise RuntimeError(
|
175
|
+
"Error in calling _check_soft_dependencies, severity argument"
|
176
|
+
f' must be "error", "warning", or "none", found {severity!r}.'
|
177
|
+
)
|
178
|
+
|
179
|
+
# if package can be imported and no version issue was caught for any string,
|
180
|
+
# then obj is compatible with the requirements and we should return True
|
181
|
+
return True
|
182
|
+
|
183
|
+
|
184
|
+
def _check_python_version(obj, package=None, msg=None, severity="error"):
|
185
|
+
"""Check if system python version is compatible with requirements of obj.
|
186
|
+
|
187
|
+
Parameters
|
188
|
+
----------
|
189
|
+
obj : BaseObject descendant
|
190
|
+
used to check python version
|
191
|
+
package : str, default = None
|
192
|
+
if given, will be used in error message as package name
|
193
|
+
msg : str, optional, default = default message (msg below)
|
194
|
+
error message to be returned in the `ModuleNotFoundError`, overrides default
|
195
|
+
severity : str, "error" (default), "warning", or "none"
|
196
|
+
whether the check should raise an error, a warning, or nothing
|
197
|
+
|
198
|
+
Returns
|
199
|
+
-------
|
200
|
+
compatible : bool, whether obj is compatible with system python version
|
201
|
+
check is using the python_version tag of obj
|
202
|
+
|
203
|
+
Raises
|
204
|
+
------
|
205
|
+
ModuleNotFoundError
|
206
|
+
User friendly error if obj has python_version tag that is
|
207
|
+
incompatible with the system python version. If package is given,
|
208
|
+
error message gives package as the reason for incompatibility.
|
209
|
+
"""
|
210
|
+
est_specifier_tag = obj.get_class_tag("python_version", tag_value_default="None")
|
211
|
+
if est_specifier_tag in ["None", None]:
|
212
|
+
return True
|
213
|
+
|
214
|
+
try:
|
215
|
+
est_specifier = SpecifierSet(est_specifier_tag)
|
216
|
+
except InvalidSpecifier:
|
217
|
+
msg_version = (
|
218
|
+
f"wrong format for python_version tag, "
|
219
|
+
f'must be PEP 440 compatible specifier string, e.g., "<3.9, >= 3.6.3",'
|
220
|
+
f" but found {est_specifier_tag!r}"
|
221
|
+
)
|
222
|
+
raise InvalidSpecifier(msg_version) from None
|
223
|
+
|
224
|
+
# python sys version, e.g., "3.8.12"
|
225
|
+
sys_version = sys.version.split(" ")[0]
|
226
|
+
|
227
|
+
if sys_version in est_specifier:
|
228
|
+
return True
|
229
|
+
# now we know that est_version is not compatible with sys_version
|
230
|
+
|
231
|
+
if not isinstance(msg, str):
|
232
|
+
msg = (
|
233
|
+
f"{type(obj).__name__} requires python version to be {est_specifier},"
|
234
|
+
f" but system python version is {sys.version}."
|
235
|
+
)
|
236
|
+
|
237
|
+
if package is not None:
|
238
|
+
msg += (
|
239
|
+
f" This is due to python version requirements of the {package} package."
|
240
|
+
)
|
241
|
+
|
242
|
+
if severity == "error":
|
243
|
+
raise ModuleNotFoundError(msg)
|
244
|
+
elif severity == "warning":
|
245
|
+
warnings.warn(msg, stacklevel=2)
|
246
|
+
elif severity == "none":
|
247
|
+
return False
|
248
|
+
else:
|
249
|
+
raise RuntimeError(
|
250
|
+
"Error in calling _check_python_version, severity "
|
251
|
+
f'argument must be "error", "warning", or "none", found {severity!r}.'
|
252
|
+
)
|
253
|
+
return True
|
skbase/utils/tests/__init__.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/env python3 -u
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
# copyright: skbase developers, BSD-3-Clause License (see LICENSE file)
|
4
|
-
"""Tests of skbase.utils functionality."""
|
1
|
+
#!/usr/bin/env python3 -u
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# copyright: skbase developers, BSD-3-Clause License (see LICENSE file)
|
4
|
+
"""Tests of skbase.utils functionality."""
|
skbase/utils/tests/test_check.py
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
#!/usr/bin/env python3 -u
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
# copyright: skbase developers, BSD-3-Clause License (see LICENSE file)
|
4
|
-
"""Tests of the utility functionality for performing various checks.
|
5
|
-
|
6
|
-
tests in this module include:
|
7
|
-
|
8
|
-
- test_is_scalar_nan_output to verify _is_scalar_nan outputs expected value for
|
9
|
-
different inputs.
|
10
|
-
"""
|
11
|
-
import numpy as np
|
12
|
-
|
13
|
-
from skbase.utils._check import _is_scalar_nan
|
14
|
-
|
15
|
-
__author__ = ["RNKuhns"]
|
16
|
-
|
17
|
-
|
18
|
-
def test_is_scalar_nan_output():
|
19
|
-
"""Test that _is_scalar_nan outputs expected value for different inputs."""
|
20
|
-
assert _is_scalar_nan(np.nan) is True
|
21
|
-
assert _is_scalar_nan(float("nan")) is True
|
22
|
-
assert _is_scalar_nan(None) is False
|
23
|
-
assert _is_scalar_nan("") is False
|
24
|
-
assert _is_scalar_nan([np.nan]) is False
|
1
|
+
#!/usr/bin/env python3 -u
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# copyright: skbase developers, BSD-3-Clause License (see LICENSE file)
|
4
|
+
"""Tests of the utility functionality for performing various checks.
|
5
|
+
|
6
|
+
tests in this module include:
|
7
|
+
|
8
|
+
- test_is_scalar_nan_output to verify _is_scalar_nan outputs expected value for
|
9
|
+
different inputs.
|
10
|
+
"""
|
11
|
+
import numpy as np
|
12
|
+
|
13
|
+
from skbase.utils._check import _is_scalar_nan
|
14
|
+
|
15
|
+
__author__ = ["RNKuhns"]
|
16
|
+
|
17
|
+
|
18
|
+
def test_is_scalar_nan_output():
|
19
|
+
"""Test that _is_scalar_nan outputs expected value for different inputs."""
|
20
|
+
assert _is_scalar_nan(np.nan) is True
|
21
|
+
assert _is_scalar_nan(float("nan")) is True
|
22
|
+
assert _is_scalar_nan(None) is False
|
23
|
+
assert _is_scalar_nan("") is False
|
24
|
+
assert _is_scalar_nan([np.nan]) is False
|