summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Peuch <cortex@worlddomination.be>2020-04-23 05:09:02 +0200
committerLaurent Peuch <cortex@worlddomination.be>2020-04-23 05:09:02 +0200
commit7ca6839754660e1cefa0544b00491cc9b023e8ab (patch)
tree4b00be0ffcb70500fc0b13208d0ae712b49bb263
parentd26a1b969f632b5511bd72c82a36fbc92ecfdee5 (diff)
downloadlogilab-common-7ca6839754660e1cefa0544b00491cc9b023e8ab.tar.gz
add deprecation.argument_removed
-rw-r--r--logilab/common/deprecation.py27
-rw-r--r--test/unittest_deprecation.py11
2 files changed, 38 insertions, 0 deletions
diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py
index 577aee7..4aaeba3 100644
--- a/logilab/common/deprecation.py
+++ b/logilab/common/deprecation.py
@@ -268,6 +268,33 @@ def argument_renamed(old_name, new_name):
return _wrap
+def argument_removed(old_argument_name):
+ """
+ callable decorator to allow getting backward compatibility for renamed keyword arguments.
+
+ >>> @argument_removed("old")
+ ... def some_function(new):
+ ... return new
+ >>> some_function(old=42)
+ sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and is deprecated, use keyword argument new instead
+ some_function(old=42)
+ 42
+ """
+ def _wrap(func):
+ @wraps(func)
+ def check_kwargs(*args, **kwargs):
+ if old_argument_name in kwargs:
+ warn(f"argument {old_argument_name} of callable {func.__name__} has been removed and is "
+ f"deprecated", stacklevel=2)
+ del kwargs[old_argument_name]
+
+ return func(*args, **kwargs)
+
+ return check_kwargs
+
+ return _wrap
+
+
def renamed(old_name, new_function):
"""use to tell that a callable has been renamed.
diff --git a/test/unittest_deprecation.py b/test/unittest_deprecation.py
index 7881252..60b10f4 100644
--- a/test/unittest_deprecation.py
+++ b/test/unittest_deprecation.py
@@ -107,6 +107,17 @@ class RawInputTC(TestCase):
with self.assertRaises(ValueError):
some_function(new=42, old=42)
+ def test_argument_removed(self):
+ @deprecation.argument_removed("old")
+ def some_function(new):
+ return new
+
+ self.assertEqual(some_function(new=42), 42)
+ self.assertEqual(some_function(new=10, old=20), 10)
+ self.assertEqual(self.messages,
+ ['argument old of callable some_function has been removed and is '
+ 'deprecated'])
+
def test_renamed(self):
def any_func():
pass