如何使用参数别名在 python 中实现弃用
How to implement deprecation in python with argument alias
我们正在开发一个 python 库,并希望更改某些函数中某些函数参数的命名方式。
我们希望保持向后兼容性,因此我们希望找到一种为函数参数创建别名的方法。
这是一个例子:
旧版本:
class MyClass(object):
def __init__(self, object_id):
self.id = object_id
新版本:
class MyClass(object):
def __init__(self, id_object):
self.id = id_object
如何让class兼容两种调用方式:
object1 = MyClass(object_id=1234)
object2 = MyClass(id_object=1234)
我当然可以创建这样的东西:
class MyClass(object):
def __init__(self, object_id=None, id_object=None):
if id_object is not None:
self.id = id_object
else:
self.id = object_id
但是,它会改变参数的数量,我们要严格避免这种情况。
有什么方法可以声明方法别名或参数别名吗?
你可以写一个装饰器:
import functools
import warnings
def deprecated_alias(**aliases):
def deco(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
rename_kwargs(f.__name__, kwargs, aliases)
return f(*args, **kwargs)
return wrapper
return deco
def rename_kwargs(func_name, kwargs, aliases):
for alias, new in aliases.items():
if alias in kwargs:
if new in kwargs:
raise TypeError('{} received both {} and {}'.format(
func_name, alias, new))
warnings.warn('{} is deprecated; use {}'.format(alias, new),
DeprecationWarning,
3)
kwargs[new] = kwargs.pop(alias)
class MyClass(object):
@deprecated_alias(object_id='id_object')
def __init__(self, id_object):
self.id = id_object
或者,由于您使用的是 Python 3,您可以将 object_id
设为仅包含关键字的参数:
import warnings
class MyClass(object):
# v Look here
def __init__(self, id_object=None, *, object_id=None):
if id_object is not None and object_id is not None:
raise TypeError("MyClass received both object_id and id_object")
elif id_object is not None:
self.id = id_object
elif object_id is not None:
warnings.warn("object_id is deprecated; use id_object", DeprecationWarning, 2)
self.id = object_id
else:
raise TypeError("MyClass missing id_object argument")
另一种不如 user2357112 的答案优雅的方法是使用元类工厂:
import warnings
def Deprecation(deprec):
class DeprecMeta(type):
def __call__(cls, *args, **kwargs):
new_kwargs = {k : v for k, v in kwargs.items() if k not in deprec}
for k in deprec:
if k in kwargs:
warnings.warn("{0}: Deprecated call with '{0}'. Use '{1}' instead".format(
cls.__name__, k, deprec[k]))
if deprec[k] not in kwargs:
new_kwargs[deprec[k]] = kwargs[k]
else:
raise TypeError('{} received both {} and {}'.format(
cls.__name__, k, deprec[k]))
obj = cls.__new__(cls, *args, **new_kwargs)
obj.__init__(*args, **new_kwargs)
return obj
return DeprecMeta
class MyClass(object, metaclass = Deprecation({'object_id': 'id_object'})):
def __init__(self, id_object):
self.id = id_object
object2 = MyClass(object_id=1234)
我们正在开发一个 python 库,并希望更改某些函数中某些函数参数的命名方式。
我们希望保持向后兼容性,因此我们希望找到一种为函数参数创建别名的方法。
这是一个例子:
旧版本:
class MyClass(object):
def __init__(self, object_id):
self.id = object_id
新版本:
class MyClass(object):
def __init__(self, id_object):
self.id = id_object
如何让class兼容两种调用方式:
object1 = MyClass(object_id=1234)
object2 = MyClass(id_object=1234)
我当然可以创建这样的东西:
class MyClass(object):
def __init__(self, object_id=None, id_object=None):
if id_object is not None:
self.id = id_object
else:
self.id = object_id
但是,它会改变参数的数量,我们要严格避免这种情况。
有什么方法可以声明方法别名或参数别名吗?
你可以写一个装饰器:
import functools
import warnings
def deprecated_alias(**aliases):
def deco(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
rename_kwargs(f.__name__, kwargs, aliases)
return f(*args, **kwargs)
return wrapper
return deco
def rename_kwargs(func_name, kwargs, aliases):
for alias, new in aliases.items():
if alias in kwargs:
if new in kwargs:
raise TypeError('{} received both {} and {}'.format(
func_name, alias, new))
warnings.warn('{} is deprecated; use {}'.format(alias, new),
DeprecationWarning,
3)
kwargs[new] = kwargs.pop(alias)
class MyClass(object):
@deprecated_alias(object_id='id_object')
def __init__(self, id_object):
self.id = id_object
或者,由于您使用的是 Python 3,您可以将 object_id
设为仅包含关键字的参数:
import warnings
class MyClass(object):
# v Look here
def __init__(self, id_object=None, *, object_id=None):
if id_object is not None and object_id is not None:
raise TypeError("MyClass received both object_id and id_object")
elif id_object is not None:
self.id = id_object
elif object_id is not None:
warnings.warn("object_id is deprecated; use id_object", DeprecationWarning, 2)
self.id = object_id
else:
raise TypeError("MyClass missing id_object argument")
另一种不如 user2357112 的答案优雅的方法是使用元类工厂:
import warnings
def Deprecation(deprec):
class DeprecMeta(type):
def __call__(cls, *args, **kwargs):
new_kwargs = {k : v for k, v in kwargs.items() if k not in deprec}
for k in deprec:
if k in kwargs:
warnings.warn("{0}: Deprecated call with '{0}'. Use '{1}' instead".format(
cls.__name__, k, deprec[k]))
if deprec[k] not in kwargs:
new_kwargs[deprec[k]] = kwargs[k]
else:
raise TypeError('{} received both {} and {}'.format(
cls.__name__, k, deprec[k]))
obj = cls.__new__(cls, *args, **new_kwargs)
obj.__init__(*args, **new_kwargs)
return obj
return DeprecMeta
class MyClass(object, metaclass = Deprecation({'object_id': 'id_object'})):
def __init__(self, id_object):
self.id = id_object
object2 = MyClass(object_id=1234)