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:
parent
1f5816ad02
commit
fe22a47e95
@ -17,6 +17,7 @@
|
||||
import inspect
|
||||
|
||||
import six
|
||||
import wrapt
|
||||
|
||||
from debtcollector import _utils
|
||||
|
||||
@ -36,9 +37,9 @@ def _moved_decorator(kind, new_attribute_name, message=None,
|
||||
if attr_postfix:
|
||||
old_attribute_name += attr_postfix
|
||||
|
||||
@six.wraps(f, assigned=_utils.get_assigned(f))
|
||||
def wrapper(self, *args, **kwargs):
|
||||
base_name = _utils.get_class_name(self, fully_qualified=False)
|
||||
@wrapt.decorator
|
||||
def wrapper(wrapped, instance, args, kwargs):
|
||||
base_name = _utils.get_class_name(wrapped, fully_qualified=False)
|
||||
if fully_qualified:
|
||||
old_name = old_attribute_name
|
||||
else:
|
||||
@ -50,9 +51,9 @@ def _moved_decorator(kind, new_attribute_name, message=None,
|
||||
version=version, removal_version=removal_version)
|
||||
_utils.deprecation(out_message, stacklevel=stacklevel,
|
||||
category=category)
|
||||
return f(self, *args, **kwargs)
|
||||
return wrapped(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
return wrapper(f)
|
||||
|
||||
return decorator
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import six
|
||||
import wrapt
|
||||
|
||||
from debtcollector import _utils
|
||||
|
||||
@ -33,17 +33,13 @@ def renamed_kwarg(old_name, new_name, message=None,
|
||||
prefix, postfix=postfix, message=message, version=version,
|
||||
removal_version=removal_version)
|
||||
|
||||
def decorator(f):
|
||||
|
||||
@six.wraps(f, assigned=_utils.get_assigned(f))
|
||||
def wrapper(*args, **kwargs):
|
||||
if old_name in kwargs:
|
||||
_utils.deprecation(out_message,
|
||||
stacklevel=stacklevel, category=category)
|
||||
if replace:
|
||||
kwargs.setdefault(new_name, kwargs.pop(old_name))
|
||||
return f(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
@wrapt.decorator
|
||||
def decorator(wrapped, instance, args, kwargs):
|
||||
if old_name in kwargs:
|
||||
_utils.deprecation(out_message,
|
||||
stacklevel=stacklevel, category=category)
|
||||
if replace:
|
||||
kwargs.setdefault(new_name, kwargs.pop(old_name))
|
||||
return wrapped(*args, **kwargs)
|
||||
|
||||
return decorator
|
||||
|
@ -357,9 +357,8 @@ class MovedMethodTest(test_base.TestCase):
|
||||
self.assertEqual(0, len(capture))
|
||||
|
||||
def test_keeps_argspec(self):
|
||||
# FIXME(blk): This should be assertEqual!
|
||||
self.assertNotEqual(inspect.getargspec(KittyKat.supermeow),
|
||||
inspect.getargspec(KittyKat.meow))
|
||||
self.assertEqual(inspect.getargspec(KittyKat.supermeow),
|
||||
inspect.getargspec(KittyKat.meow))
|
||||
|
||||
|
||||
class RenamedKwargTest(test_base.TestCase):
|
||||
@ -417,10 +416,8 @@ class RenamedKwargTest(test_base.TestCase):
|
||||
|
||||
def test_argspec(self):
|
||||
# The decorated function keeps its argspec.
|
||||
|
||||
# FIXME(bknudson): This isn't working right, should be assertEqual!
|
||||
self.assertNotEqual(inspect.getargspec(blip_blop_unwrapped),
|
||||
inspect.getargspec(blip_blop))
|
||||
self.assertEqual(inspect.getargspec(blip_blop_unwrapped),
|
||||
inspect.getargspec(blip_blop))
|
||||
|
||||
|
||||
class UpdatedArgsTest(test_base.TestCase):
|
||||
@ -441,9 +438,8 @@ class UpdatedArgsTest(test_base.TestCase):
|
||||
self.assertEqual(0, len(capture))
|
||||
|
||||
def test_argspec_preserved(self):
|
||||
# FIXME(bknudson): This should be assertEqual!
|
||||
self.assertNotEqual(inspect.getargspec(blip_blop_blip_unwrapped),
|
||||
inspect.getargspec(blip_blop_blip))
|
||||
self.assertEqual(inspect.getargspec(blip_blop_blip_unwrapped),
|
||||
inspect.getargspec(blip_blop_blip))
|
||||
|
||||
|
||||
class RemovalTests(test_base.TestCase):
|
||||
|
@ -15,6 +15,7 @@
|
||||
# under the License.
|
||||
|
||||
import six
|
||||
import wrapt
|
||||
if six.PY3:
|
||||
import inspect
|
||||
Parameter = inspect.Parameter
|
||||
@ -50,8 +51,8 @@ def updated_kwarg_default_value(name, old_value, new_value, message=None,
|
||||
sig = get_signature(f)
|
||||
varnames = list(six.iterkeys(sig.parameters))
|
||||
|
||||
@six.wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
@wrapt.decorator
|
||||
def wrapper(wrapped, instance, args, kwargs):
|
||||
explicit_params = set(
|
||||
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:
|
||||
_utils.deprecation(out_message,
|
||||
stacklevel=stacklevel, category=category)
|
||||
return f(*args, **kwargs)
|
||||
return wrapped(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
return wrapper(f)
|
||||
|
||||
return decorator
|
||||
|
Loading…
Reference in New Issue
Block a user