From dfcc38f9aa74b8dc579966fb91fbd5c6f3c4eb29 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 22 Apr 2020 22:53:17 +0200 Subject: [deprecation/refactoring] simplify moved --- logilab/common/deprecation.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'logilab') 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): -- cgit v1.2.1