deprecator#
Function for recording and reporting deprecations.
Notes
This file is copied (with minor modifications) from the Nibabel. nipy/nibabel. See COPYING file distributed along with the Nibabel package for the copyright and license terms.
Error for expired deprecation. |
|
Warning for args deprecation. |
|
|
Compare |
|
Return True if version_str is too high. |
|
Return decorator function for deprecation warning or error. |
|
Deprecate a renamed or removed function argument. |
ExpiredDeprecationError#
- class fury.deprecator.ExpiredDeprecationError[source]#
Bases:
RuntimeErrorError for expired deprecation.
Error raised when a called function or method has passed out of its deprecation period.
- __init__(*args, **kwargs)#
ArgsDeprecationWarning#
- class fury.deprecator.ArgsDeprecationWarning[source]#
Bases:
DeprecationWarningWarning for args deprecation.
Warning raised when a function or method argument has changed or removed.
- __init__(*args, **kwargs)#
cmp_pkg_version#
- fury.deprecator.cmp_pkg_version(version_str, pkg_version_str='2.1.0.dev63+g1a8fd7626')[source]#
Compare
version_strto current package version.- Parameters:
- Returns:
1 if
version_stris 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
is_bad_version#
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 or error.
The decorated function or method will:
Raise the given
warning_classwarning when the function or method gets called, up to (and including) version until (if specified).Raise the given
error_classerror when the function or 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, optional) – 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_comparatormay compare the input version string to the current package version string.warn_class (class, optional) – Class of warning to generate for deprecation. Default is DeprecationWarning.
error_class (class, optional) – Class of error to generate when
version_comparatorreturns 1 for a given argument ofuntil. Default is ExpiredDeprecationError.
- Returns:
Function returning a decorator.
- Return type:
callable
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_namewas removed from the function signature and thenew_namereplaced it at the same position in the signature. If theold_nameargument is given when calling the decorated function the decorator will catch it and issue a deprecation warning and pass it on asnew_nameargument.- Parameters:
old_name (str or list/tuple) – The old name of the argument.
new_name (str or list/tuple or None, optional) – The new name of the argument. Set this to None to remove the argument
old_nameinstead of renaming it.since (str or number or list/tuple, optional) – The release at which the old argument became deprecated.
until (str or number or list/tuple, 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, optional) – 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_comparatormay compare the input version string to the current package version string.arg_in_kwargs (bool or list/tuple, 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_namecannot be found in the signature of the decorated function. Default isFalse.warn_class (class, optional) – Warning to be issued. Default is ArgsDeprecationWarning.
error_class (class, optional) – Error to be issued. Default is ExpiredDeprecationError.
alternative (str, optional) – An alternative function or class name that the user may use in place of the deprecated object if
new_nameis None. The deprecation warning will tell the user about this alternative if provided.
- Returns:
Function that can be used as a decorator.
- Return type:
callable
- 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). 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_nameandsincehave 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)