python@deprecated decorator不支持旧的python类、函数或方法。
Deprecated的Python项目详细描述
不推荐使用很容易使用
如果需要将函数或方法标记为已弃用, 您可以使用@deprecateddecorator:
保存在hello.py:
fromdeprecatedimportdeprecated@deprecated(version='1.2.1',reason="You should use another function")defsome_old_function(x,y):returnx+yclassSomeClass(object):@deprecated(version='1.3.0',reason="This method is deprecated")defsome_old_method(self,x,y):returnx+ysome_old_function(12,34)obj=SomeClass()obj.some_old_method(5,8)
而且易于设置
运行它:
$ pip install Deprecated $ python hello.py hello.py:15: DeprecationWarning: Call to deprecated function(or staticmethod) some_old_function. (You should use another function) -- Deprecated since version 1.2.0. some_old_function(12, 34) hello.py:17: DeprecationWarning: Call to deprecated method some_old_method. (This method is deprecated) -- Deprecated since version 1.3.0. obj.some_old_method(5, 8)
您可以记录代码
您是否想过如何记录某些函数、类、方法等已弃用? 现在可以使用集成的sphinx指令执行此操作:
例如,在hello_sphinx.py:
fromdeprecated.sphinximportdeprecatedfromdeprecated.sphinximportversionaddedfromdeprecated.sphinximportversionchanged@versionadded(version='1.0',reason="This function is new")deffunction_one():'''This is the function one'''@versionchanged(version='1.0',reason="This function is modified")deffunction_two():'''This is the function two'''@deprecated(version='1.0',reason="This function will be removed soon")deffunction_three():'''This is the function three'''function_one()function_two()function_three()# warnshelp(function_one)help(function_two)help(function_three)
结果立即生效
运行:
$ python hello_sphinx.py hello_sphinx.py:23: DeprecationWarning: Call to deprecated function(or staticmethod) function_three. (This function will be removed soon) -- Deprecated since version 1.0. function_three()# warns Help on function function_one in module __main__: function_one() This is the function one .. versionadded:: 1.0 This function is new Help on function function_two in module __main__: function_two() This is the function two .. versionchanged:: 1.0 This function is modified Help on function function_three in module __main__: function_three() This is the function three .. deprecated:: 1.0 This function will be removed soon