deprecator
¶
Function for recording and reporting deprecations.
Notes
this file is copied (with minor modifications) from the Nibabel. https://github.com/nipy/nibabel. See COPYING file distributed along with the Nibabel package for the copyright and license terms.
Warning for args deprecation. |
|
Error for expired deprecation. |
|
|
Compare |
|
Return decorator function for deprecation warning / error. |
|
Deprecate a renamed or removed function argument. |
|
Return True if version_str is too high. |
|
Return package-like thing and module setup for package name. |
|
Get a signature object for the passed callable. |
ArgsDeprecationWarning
¶
-
class
fury.deprecator.
ArgsDeprecationWarning
[source]¶ Bases:
DeprecationWarning
Warning for args deprecation.
Warning raised when a function or method argument has changed or removed.
-
__init__
()¶ Initialize self. See help(type(self)) for accurate signature.
-
ExpiredDeprecationError
¶
-
class
fury.deprecator.
ExpiredDeprecationError
[source]¶ Bases:
RuntimeError
Error for expired deprecation.
Error raised when a called function or method has passed out of its deprecation period.
-
__init__
()¶ Initialize self. See help(type(self)) for accurate signature.
-
cmp_pkg_version¶
-
fury.deprecator.
cmp_pkg_version
(version_str, pkg_version_str='0.7.1')[source]¶ Compare
version_str
to current package version.- Parameters
- Returns
version_cmp – 1 if
version_str
is a later version thanpkg_version_str
, 0 if same, -1 if earlier.- Return type
Examples
>>> cmp_pkg_version('1.2.1', '1.2.0') 1 >>> cmp_pkg_version('1.2.0dev', '1.2.0') -1
deprecate_with_version¶
-
fury.deprecator.
deprecate_with_version
(message, since='', until='', version_comparator=<function cmp_pkg_version>, warn_class=<class 'DeprecationWarning'>, error_class=<class 'fury.deprecator.ExpiredDeprecationError'>)[source]¶ Return decorator function for deprecation warning / error.
The decorated function / method will:
Raise the given
warning_class
warning when the function / method gets called, up to (and including) version until (if specified);Raise the given
error_class
error when the function / method gets called, when the package version is greater than versionuntil
(if specified).
- Parameters
message (str) – Message explaining deprecation, giving possible alternatives.
since (str, optional) – Released version at which object was first deprecated.
until (str, optional) – Last released version at which this function will still raise a deprecation warning. Versions higher than this will raise an error.
version_comparator (callable) – Callable accepting string as argument, and return 1 if string represents a higher version than encoded in the version_comparator`, 0 if the version is equal, and -1 if the version is lower. For example, the
version_comparator
may compare the input version string to the current package version string.warn_class (class, optional) – Class of warning to generate for deprecation.
error_class (class, optional) – Class of error to generate when
version_comparator
returns 1 for a given argument ofuntil
.
- Returns
deprecator – Function returning a decorator.
- Return type
func
deprecated_params¶
-
fury.deprecator.
deprecated_params
(old_name, new_name=None, since='', until='', version_comparator=<function cmp_pkg_version>, arg_in_kwargs=False, warn_class=<class 'fury.deprecator.ArgsDeprecationWarning'>, error_class=<class 'fury.deprecator.ExpiredDeprecationError'>, alternative='')[source]¶ Deprecate a renamed or removed function argument.
The decorator assumes that the argument with the
old_name
was removed from the function signature and thenew_name
replaced it at the same position in the signature. If theold_name
argument is given when calling the decorated function the decorator will catch it and issue a deprecation warning and pass it on asnew_name
argument.- Parameters
old_name (str or list/tuple thereof) – The old name of the argument.
new_name (str or list/tuple thereof or
None
, optional) – The new name of the argument. Set this to None to remove the argumentold_name
instead of renaming it.since (str or number or list/tuple thereof, optional) – The release at which the old argument became deprecated.
until (str or number or list/tuple thereof, optional) – Last released version at which this function will still raise a deprecation warning. Versions higher than this will raise an error.
version_comparator (callable) – Callable accepting string as argument, and return 1 if string represents a higher version than encoded in the
version_comparator
, 0 if the version is equal, and -1 if the version is lower. For example, theversion_comparator
may compare the input version string to the current package version string.arg_in_kwargs (bool or list/tuple thereof, optional) – If the argument is not a named argument (for example it was meant to be consumed by
**kwargs
) set this toTrue
. Otherwise the decorator will throw an Exception if thenew_name
cannot be found in the signature of the decorated function. Default isFalse
.warn_class (warning, optional) – Warning to be issued.
error_class (Exception, optional) – Error to be issued
alternative (str, optional) – An alternative function or class name that the user may use in place of the deprecated object if
new_name
is None. The deprecation warning will tell the user about this alternative if provided.
- Raises
TypeError – If the new argument name cannot be found in the function signature and arg_in_kwargs was False or if it is used to deprecate the name of the
*args
-,**kwargs
-like arguments. At runtime such an Error is raised if both the new_name and old_name were specified when calling the function and “relax=False”.
Notes
This function is based on the Astropy (major modification). https://github.com/astropy/astropy. See COPYING file distributed along with the astropy package for the copyright and license terms.
Examples
The deprecation warnings are not shown in the following examples. To deprecate a positional or keyword argument:: >>> from fury.deprecator import deprecated_params >>> @deprecated_params(‘sig’, ‘sigma’, ‘0.3’) … def test(sigma): … return sigma >>> test(2) 2 >>> test(sigma=2) 2 >>> test(sig=2) # doctest: +SKIP 2
It is also possible to replace multiple arguments. The
old_name
,new_name
andsince
have to be tuple or list and contain the same number of entries:: >>> @deprecated_params([‘a’, ‘b’], [‘alpha’, ‘beta’], … [‘0.2’, 0.4]) … def test(alpha, beta): … return alpha, beta >>> test(a=2, b=3) # doctest: +SKIP (2, 3)
is_bad_version¶
optional_package¶
-
fury.deprecator.
optional_package
(name, trip_msg=None)[source]¶ Return package-like thing and module setup for package name.
- Parameters
- Returns
pkg_like (module or
TripWire
instance) – If we can import the package, return it. Otherwise return an object raising an error when accessedhave_pkg (bool) – True if import for package was successful, false otherwise
module_setup (function) – callable usually set as
setup_module
in calling namespace, to allow skipping tests.
Examples
Typical use would be something like this at the top of a module using an optional package: >>> from fury.optpkg import optional_package >>> pkg, have_pkg, setup_module = optional_package(‘not_a_package’) Of course in this case the package doesn’t exist, and so, in the module: >>> have_pkg False and >>> pkg.some_function() #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): … TripWireError: We need package not_a_package for these functions, but
import not_a_package
raised an ImportError If the module does exist - we get the module >>> pkg, _, _ = optional_package(‘os’) >>> hasattr(pkg, ‘path’) True Or a submodule if that’s what we asked for >>> subpkg, _, _ = optional_package(‘os.path’) >>> hasattr(subpkg, ‘dirname’) True