Fix renamed_kwarg to preserve argspec

renamed_kwarg is fixed so that the function reports the original
argspec.

Change-Id: I0ef289c27c6f754afc16985164aa90a4a04867b3
This commit is contained in:
Brant Knudson 2016-04-13 16:02:28 -05:00
parent 1f5816ad02
commit fe22a47e95
4 changed files with 26 additions and 32 deletions

View File

@ -17,6 +17,7 @@
import inspect import inspect
import six import six
import wrapt
from debtcollector import _utils from debtcollector import _utils
@ -36,9 +37,9 @@ def _moved_decorator(kind, new_attribute_name, message=None,
if attr_postfix: if attr_postfix:
old_attribute_name += attr_postfix old_attribute_name += attr_postfix
@six.wraps(f, assigned=_utils.get_assigned(f)) @wrapt.decorator
def wrapper(self, *args, **kwargs): def wrapper(wrapped, instance, args, kwargs):
base_name = _utils.get_class_name(self, fully_qualified=False) base_name = _utils.get_class_name(wrapped, fully_qualified=False)
if fully_qualified: if fully_qualified:
old_name = old_attribute_name old_name = old_attribute_name
else: else:
@ -50,9 +51,9 @@ def _moved_decorator(kind, new_attribute_name, message=None,
version=version, removal_version=removal_version) version=version, removal_version=removal_version)
_utils.deprecation(out_message, stacklevel=stacklevel, _utils.deprecation(out_message, stacklevel=stacklevel,
category=category) category=category)
return f(self, *args, **kwargs) return wrapped(*args, **kwargs)
return wrapper return wrapper(f)
return decorator return decorator

View File

@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import six import wrapt
from debtcollector import _utils from debtcollector import _utils
@ -33,17 +33,13 @@ def renamed_kwarg(old_name, new_name, message=None,
prefix, postfix=postfix, message=message, version=version, prefix, postfix=postfix, message=message, version=version,
removal_version=removal_version) removal_version=removal_version)
def decorator(f): @wrapt.decorator
def decorator(wrapped, instance, args, kwargs):
@six.wraps(f, assigned=_utils.get_assigned(f)) if old_name in kwargs:
def wrapper(*args, **kwargs): _utils.deprecation(out_message,
if old_name in kwargs: stacklevel=stacklevel, category=category)
_utils.deprecation(out_message, if replace:
stacklevel=stacklevel, category=category) kwargs.setdefault(new_name, kwargs.pop(old_name))
if replace: return wrapped(*args, **kwargs)
kwargs.setdefault(new_name, kwargs.pop(old_name))
return f(*args, **kwargs)
return wrapper
return decorator return decorator

View File

@ -357,9 +357,8 @@ class MovedMethodTest(test_base.TestCase):
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
def test_keeps_argspec(self): def test_keeps_argspec(self):
# FIXME(blk): This should be assertEqual! self.assertEqual(inspect.getargspec(KittyKat.supermeow),
self.assertNotEqual(inspect.getargspec(KittyKat.supermeow), inspect.getargspec(KittyKat.meow))
inspect.getargspec(KittyKat.meow))
class RenamedKwargTest(test_base.TestCase): class RenamedKwargTest(test_base.TestCase):
@ -417,10 +416,8 @@ class RenamedKwargTest(test_base.TestCase):
def test_argspec(self): def test_argspec(self):
# The decorated function keeps its argspec. # The decorated function keeps its argspec.
self.assertEqual(inspect.getargspec(blip_blop_unwrapped),
# FIXME(bknudson): This isn't working right, should be assertEqual! inspect.getargspec(blip_blop))
self.assertNotEqual(inspect.getargspec(blip_blop_unwrapped),
inspect.getargspec(blip_blop))
class UpdatedArgsTest(test_base.TestCase): class UpdatedArgsTest(test_base.TestCase):
@ -441,9 +438,8 @@ class UpdatedArgsTest(test_base.TestCase):
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
def test_argspec_preserved(self): def test_argspec_preserved(self):
# FIXME(bknudson): This should be assertEqual! self.assertEqual(inspect.getargspec(blip_blop_blip_unwrapped),
self.assertNotEqual(inspect.getargspec(blip_blop_blip_unwrapped), inspect.getargspec(blip_blop_blip))
inspect.getargspec(blip_blop_blip))
class RemovalTests(test_base.TestCase): class RemovalTests(test_base.TestCase):

View File

@ -15,6 +15,7 @@
# under the License. # under the License.
import six import six
import wrapt
if six.PY3: if six.PY3:
import inspect import inspect
Parameter = inspect.Parameter Parameter = inspect.Parameter
@ -50,8 +51,8 @@ def updated_kwarg_default_value(name, old_value, new_value, message=None,
sig = get_signature(f) sig = get_signature(f)
varnames = list(six.iterkeys(sig.parameters)) varnames = list(six.iterkeys(sig.parameters))
@six.wraps(f) @wrapt.decorator
def wrapper(*args, **kwargs): def wrapper(wrapped, instance, args, kwargs):
explicit_params = set( explicit_params = set(
varnames[:len(args)] + list(kwargs.keys()) varnames[:len(args)] + list(kwargs.keys())
) )
@ -60,8 +61,8 @@ def updated_kwarg_default_value(name, old_value, new_value, message=None,
if name in default_params: if name in default_params:
_utils.deprecation(out_message, _utils.deprecation(out_message,
stacklevel=stacklevel, category=category) stacklevel=stacklevel, category=category)
return f(*args, **kwargs) return wrapped(*args, **kwargs)
return wrapper return wrapper(f)
return decorator return decorator