deprecator

Function for recording and reporting deprecations.

Note

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.

ExpiredDeprecationError

Error for expired deprecation.

cmp_pkg_version(version_str[, pkg_version_str])

Compare version_str to current package version.

deprecate_with_version(message[, since, …])

Return decorator function function for deprecation warning / error.

optional_package(name[, trip_msg])

Return package-like thing and module setup for package name.

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.5.1')[source]

Compare version_str to current package version.

Parameters
  • version_str (str) – Version string to compare to current package version

  • pkg_version_str (str, optional) – Version of our package. Optional, set fom __version__ by default.

Returns

version_cmp – 1 if version_str is a later version than pkg_version_str, 0 if same, -1 if earlier.

Return type

int

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 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 version until (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 of until.

Returns

deprecator – Function returning a decorator.

Return type

func

optional_package

fury.deprecator.optional_package(name, trip_msg=None)[source]

Return package-like thing and module setup for package name.

Parameters
  • name (str) – package name

  • trip_msg (None or str) – message to give when someone tries to use the return package, but we could not import it, and have returned a TripWire object instead. Default message if None.

Returns

  • pkg_like (module or TripWire instance) – If we can import the package, return it. Otherwise return an object raising an error when accessed

  • have_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