diff options
-rw-r--r-- | logilab/common/deprecation.py | 23 | ||||
-rw-r--r-- | test/unittest_deprecation.py | 2 |
2 files changed, 21 insertions, 4 deletions
diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py index cdf538c..636b498 100644 --- a/logilab/common/deprecation.py +++ b/logilab/common/deprecation.py @@ -283,11 +283,28 @@ class class_deprecated(type): return type.__call__(cls, *args, **kwargs) -def moved(modpath, objname): - return _defaultdeprecator.moved(None, modpath, objname) +def moved(modpath, objname, version=None, stacklevel=2): + """use to tell that a callable has been moved to a new module. + It returns a callable wrapper, so that when its called a warning is printed + telling where the object can be found, import is done (and not before) and + the actual object is called. + + NOTE: the usage is somewhat limited on classes since it will fail if the + wrapper is use in a class ancestors list, use the `class_moved` function + instead (which has no lazy import feature though). + """ + message = "object %s has been moved to module %s" % (objname, modpath) + + def callnew(*args, **kwargs): + from logilab.common.modutils import load_module_from_name + + send_warning(message, version=version, stacklevel=stacklevel + 1) + + m = load_module_from_name(modpath) + return getattr(m, objname)(*args, **kwargs) -moved.__doc__ = _defaultdeprecator.moved.__doc__ + return callnew def attribute_renamed(old_name, new_name): diff --git a/test/unittest_deprecation.py b/test/unittest_deprecation.py index 09902a2..5884235 100644 --- a/test/unittest_deprecation.py +++ b/test/unittest_deprecation.py @@ -133,7 +133,7 @@ class RawInputTC(TestCase): any_func = deprecation.moved(module, 'moving_target') any_func() self.assertEqual(self.messages, - ['object moving_target has been moved to module data.deprecation']) + ['[logilab.common] object moving_target has been moved to module data.deprecation']) def test_deprecated_manager(self): deprecator = deprecation.DeprecationManager("module_name") |