summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Peuch <cortex@worlddomination.be>2020-04-23 04:00:22 +0200
committerLaurent Peuch <cortex@worlddomination.be>2020-04-23 04:00:22 +0200
commitf502fe0496b292b433c6b2299698a180769c79c6 (patch)
tree52d774851015fc01f55417e5da11de65a58c88c8
parent9ec44ee898ee827daab19265314117f27bc648de (diff)
downloadlogilab-common-f502fe0496b292b433c6b2299698a180769c79c6.tar.gz
[deprecation/refactoring] remove totally overkill DeprecationManager
-rw-r--r--logilab/common/deprecation.py139
-rw-r--r--test/unittest_deprecation.py60
2 files changed, 0 insertions, 199 deletions
diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py
index 7fb2c1d..668d3e5 100644
--- a/logilab/common/deprecation.py
+++ b/logilab/common/deprecation.py
@@ -24,8 +24,6 @@ import sys
from warnings import warn
from functools import wraps
-from logilab.common.changelog import Version
-
class DeprecationWrapper(object):
"""proxy to print a warning on access to any attribute of the wrapped object
@@ -46,143 +44,6 @@ class DeprecationWrapper(object):
setattr(self._proxied, attr, value)
-class DeprecationManager(object):
- """Manage the deprecation message handling. Messages are dropped for
- versions more recent than the 'compatible' version. Example::
-
- deprecator = deprecation.DeprecationManager("module_name")
- deprecator.compatibility('1.3')
-
- deprecator.warn('1.2', "message.")
-
- @deprecator.deprecated('1.2', 'Message')
- def any_func():
- pass
-
- class AnyClass(object):
- __metaclass__ = deprecator.class_deprecated('1.2')
- """
- def __init__(self, module_name=None):
- """
- """
- self.module_name = module_name
- self.compatible_version = None
-
- def compatibility(self, compatible_version):
- """Set the compatible version.
- """
- self.compatible_version = Version(compatible_version)
-
- def deprecated(self, version=None, reason=None, stacklevel=2, name=None, doc=None):
- """Display a deprecation message only if the version is older than the
- compatible version.
- """
- def decorator(func):
- message = reason or 'The function "%s" is deprecated'
- if '%s' in message:
- message %= func.__name__
-
- @wraps(func)
- def wrapped(*args, **kwargs):
- self.warn(version, message, stacklevel+1)
- return func(*args, **kwargs)
- return wrapped
- return decorator
-
- def class_deprecated(self, version=None):
- class metaclass(type):
- """metaclass to print a warning on instantiation of a deprecated class"""
-
- def __call__(cls, *args, **kwargs):
- msg = getattr(cls, "__deprecation_warning__",
- "%(cls)s is deprecated") % {'cls': cls.__name__}
- self.warn(version, msg, stacklevel=3)
- return type.__call__(cls, *args, **kwargs)
- return metaclass
-
- def moved(self, version, modpath, objname):
- """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).
- """
- def callnew(*args, **kwargs):
- from logilab.common.modutils import load_module_from_name
- message = "object %s has been moved to module %s" % (objname, modpath)
- self.warn(version, message)
- m = load_module_from_name(modpath)
- return getattr(m, objname)(*args, **kwargs)
- return callnew
-
- def renamed(self, old_name, new_function):
- """use to tell that a callable has been renamed.
-
- It returns a callable wrapper, so that when its called a warning is printed
- telling what is the object new name.
- """
- @wraps(new_function)
- def wrapped(*args, **kwargs):
- self.warn(None, (
- f"{old_name} has been renamed and is deprecated, uses {new_function.__name__} "
- f"instead"
- ), stacklevel=3)
- return new_function(*args, **kwargs)
- return wrapped
-
- def class_renamed(self, version, old_name, new_class, message=None):
- clsdict = {}
- if message is None:
- message = '%s is deprecated, use %s' % (old_name, new_class.__name__)
- clsdict['__deprecation_warning__'] = message
- try:
- # new-style class
- return self.class_deprecated(version)(old_name, (new_class,), clsdict)
- except (NameError, TypeError):
- # old-style class
- warn = self.warn
- class DeprecatedClass(new_class):
- """FIXME: There might be a better way to handle old/new-style class
- """
- def __init__(self, *args, **kwargs):
- warn(version, message, stacklevel=3)
- new_class.__init__(self, *args, **kwargs)
- return DeprecatedClass
-
- def class_moved(self, version, new_class, old_name=None, message=None):
- """nice wrapper around class_renamed when a class has been moved into
- another module
- """
- if old_name is None:
- old_name = new_class.__name__
- if message is None:
- message = 'class %s is now available as %s.%s' % (
- old_name, new_class.__module__, new_class.__name__)
- return self.class_renamed(version, old_name, new_class, message)
-
- def warn(self, version=None, reason="", stacklevel=2):
- """Display a deprecation message only if the version is older than the
- compatible version.
- """
- if (self.compatible_version is None
- or version is None
- or Version(version) < self.compatible_version):
- if self.module_name and version:
- reason = '[%s %s] %s' % (self.module_name, version, reason)
- elif self.module_name:
- reason = '[%s] %s' % (self.module_name, reason)
- elif version:
- reason = '[%s] %s' % (version, reason)
- warn(reason, DeprecationWarning, stacklevel=stacklevel)
-
-
-_defaultdeprecator = DeprecationManager()
-
-
def _get_package_name(number=2):
"""
automagically try to determine the package name from which the warning has
diff --git a/test/unittest_deprecation.py b/test/unittest_deprecation.py
index 5432823..dfe6b67 100644
--- a/test/unittest_deprecation.py
+++ b/test/unittest_deprecation.py
@@ -162,66 +162,6 @@ class RawInputTC(TestCase):
self.assertEqual(self.messages,
['[logilab.common] object moving_target has been moved to module data.deprecation'])
- def test_deprecated_manager(self):
- deprecator = deprecation.DeprecationManager("module_name")
- deprecator.compatibility('1.3')
- # This warn should be printed.
- deprecator.warn('1.1', "Major deprecation message.", 1)
- deprecator.warn('1.1')
-
- @deprecator.deprecated('1.2', 'Major deprecation message.')
- def any_func():
- pass
- any_func()
-
- @deprecator.deprecated('1.2')
- def other_func():
- pass
- other_func()
-
- self.assertListEqual(self.messages,
- ['[module_name 1.1] Major deprecation message.',
- '[module_name 1.1] ',
- '[module_name 1.2] Major deprecation message.',
- '[module_name 1.2] The function "other_func" is deprecated'])
-
- def test_class_deprecated_manager(self):
- deprecator = deprecation.DeprecationManager("module_name")
- deprecator.compatibility('1.3')
- class AnyClass(object, metaclass=deprecator.class_deprecated('1.2')):
- pass
- AnyClass()
- self.assertEqual(self.messages,
- ['[module_name 1.2] AnyClass is deprecated'])
-
-
- def test_deprecated_manager_noprint(self):
- deprecator = deprecation.DeprecationManager("module_name")
- deprecator.compatibility('1.3')
- # This warn should not be printed.
- deprecator.warn('1.3', "Minor deprecation message.", 1)
-
- @deprecator.deprecated('1.3', 'Minor deprecation message.')
- def any_func():
- pass
- any_func()
-
- @deprecator.deprecated('1.20')
- def other_func():
- pass
- other_func()
-
- @deprecator.deprecated('1.4')
- def other_func():
- pass
- other_func()
-
- class AnyClass(object):
- __metaclass__ = deprecator.class_deprecated((1,5))
- AnyClass()
-
- self.assertFalse(self.messages)
-
if __name__ == '__main__':
unittest_main()