Commit 1fa8a2ad authored by Laurent Peuch's avatar Laurent Peuch
Browse files

add deprecation.argument_removed

parent d1ef73454443
......@@ -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.
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment