summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>2009-07-31 21:02:04 +0200
committerNicolas Chauvat <nicolas.chauvat@logilab.fr>2009-07-31 21:02:04 +0200
commitbefdfbd9e58024421599c45b7ea1ea53fc001e14 (patch)
treeaab19040f033d94d8b0cfd2298d17bdc1b0e9a25
parent117d91a95f8cc2e16525ae372210c6a7e3dbfc31 (diff)
downloadlogilab-common-befdfbd9e58024421599c45b7ea1ea53fc001e14.tar.gz
R [deprecation] renaming and simplifying (impacts deprecated, obsolete)
-rw-r--r--deprecation.py38
-rw-r--r--test/__init__.py0
-rw-r--r--test/unittest_deprecation.py55
3 files changed, 72 insertions, 21 deletions
diff --git a/deprecation.py b/deprecation.py
index bbb7cda..f3c3d76 100644
--- a/deprecation.py
+++ b/deprecation.py
@@ -9,8 +9,7 @@ __docformat__ = "restructuredtext en"
import sys
from warnings import warn
-
-class deprecated(type):
+class class_deprecated(type):
"""metaclass to print a warning on instantiation of a deprecated class"""
def __call__(cls, *args, **kwargs):
@@ -59,8 +58,21 @@ def class_moved(new_class, old_name=None, message=None):
old_name, new_class.__module__, new_class.__name__)
return class_renamed(old_name, new_class, message)
+def deprecated(reason=None):
+ """Decorator that raises a DeprecationWarning to print a message
+ when the decorated function is called.
+ """
+ def deprecated_decorator(func):
+ message = reason or 'this function is deprecated, use %s instead'
+ if '%s' in message:
+ message = message % func.func_name
+ def wrapped(*args, **kwargs):
+ warn(message, DeprecationWarning, stacklevel=2)
+ return func(*args, **kwargs)
+ return wrapped
+ return deprecated_decorator
-def deprecated_function(new_func, message=None):
+def deprecated_function(func, message=None):
"""Creates a function which fires a DeprecationWarning when used.
For example, if <bar> is deprecated in favour of <foo>:
@@ -71,14 +83,7 @@ def deprecated_function(new_func, message=None):
bar()
>>>
"""
- if message is None:
- message = "this function is deprecated, use %s instead" % (
- new_func.func_name)
- def deprecated(*args, **kwargs):
- warn(message, DeprecationWarning, stacklevel=2)
- return new_func(*args, **kwargs)
- return deprecated
-
+ return deprecated(message)(func)
def moved(modpath, objname):
"""use to tell that a callable has been moved to a new module.
@@ -99,14 +104,5 @@ def moved(modpath, objname):
return getattr(m, objname)(*args, **kwargs)
return callnew
-def obsolete(reason="This function is obsolete"):
- """this function is an alternative to `deprecated_function`
- when there's no real replacement for the deprecated function
- """
- def newdecorator(func):
- def wrapped(*args, **kwargs):
- warn(reason, DeprecationWarning, stacklevel=2)
- return func(*args, **kwargs)
- return wrapped
- return newdecorator
+obsolete = deprecated_function(deprecated, 'obsolete is deprecated, use deprecated instead')
diff --git a/test/__init__.py b/test/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/__init__.py
diff --git a/test/unittest_deprecation.py b/test/unittest_deprecation.py
new file mode 100644
index 0000000..f03585b
--- /dev/null
+++ b/test/unittest_deprecation.py
@@ -0,0 +1,55 @@
+"""unit tests for logilab.common.deprecation"""
+
+import warnings
+
+from logilab.common.testlib import TestCase, unittest_main
+from logilab.common import deprecation
+
+def moving_target():
+ pass
+
+class RawInputTC(TestCase):
+
+ # XXX with 2.6 we could test warnings
+ # http://docs.python.org/library/warnings.html#testing-warnings
+ # instead we just make sure it does not crash
+ def setUp(self):
+ warnings.simplefilter("ignore")
+ def tearDown(self):
+ warnings.simplefilter("default")
+
+ def test_class_deprecated(self):
+ class AnyClass:
+ __metaclass__ = deprecation.class_deprecated
+
+ def test_deprecated_function(self):
+ def any_func():
+ pass
+ any_func = deprecation.deprecated_function(any_func)
+ any_func()
+ any_func = deprecation.deprecated_function(any_func,'message')
+ any_func()
+ @deprecation.deprecated_function
+ def any_func():
+ pass
+ any_func()
+
+ def test_deprecated(self):
+ @deprecation.deprecated()
+ def any_func():
+ pass
+ any_func()
+
+ @deprecation.deprecated('message')
+ def any_func():
+ pass
+ any_func()
+
+ def test_moved(self):
+ # this test needs l.c.test.__init__
+ module = 'logilab.common.test.unittest_deprecation'
+ any_func = deprecation.moved(module, 'moving_target')
+ any_func()
+
+if __name__ == '__main__':
+ unittest_main()