我需要将例程标记为已弃用,但显然没有用于弃用的标准库装饰器。我知道它的配方和警告模块,但我的问题是:为什么没有标准的库装饰器为这个(常见)任务?

另外一个问题:标准库中到底有没有标准装饰器?


当前回答

我猜原因是Python代码不能被静态处理(就像c++编译器那样),在实际使用某些东西之前,你不能得到警告。我不认为这是一个好主意垃圾邮件用户你的脚本的一堆消息“警告:这个脚本的开发人员正在使用弃用的API”。

更新:但你可以创建装饰器,将原来的功能转换成另一个。新函数将标记/检查开关,告诉该函数已被调用,并仅在开关进入打开状态时显示消息。和/或在退出时,它可以打印程序中使用的所有弃用函数的列表。

其他回答

我猜原因是Python代码不能被静态处理(就像c++编译器那样),在实际使用某些东西之前,你不能得到警告。我不认为这是一个好主意垃圾邮件用户你的脚本的一堆消息“警告:这个脚本的开发人员正在使用弃用的API”。

更新:但你可以创建装饰器,将原来的功能转换成另一个。新函数将标记/检查开关,告诉该函数已被调用,并仅在开关进入打开状态时显示消息。和/或在退出时,它可以打印程序中使用的所有弃用函数的列表。

以下是一些摘录,是根据Leandro引用的内容修改的:

import warnings
import functools

def deprecated(func):
    """This is a decorator which can be used to mark functions
    as deprecated. It will result in a warning being emitted
    when the function is used."""
    @functools.wraps(func)
    def new_func(*args, **kwargs):
        warnings.simplefilter('always', DeprecationWarning)  # turn off filter
        warnings.warn("Call to deprecated function {}.".format(func.__name__),
                      category=DeprecationWarning,
                      stacklevel=2)
        warnings.simplefilter('default', DeprecationWarning)  # reset filter
        return func(*args, **kwargs)
    return new_func

# Examples

@deprecated
def some_old_function(x, y):
    return x + y

class SomeClass:
    @deprecated
    def some_old_method(self, x, y):
        return x + y

因为在一些解释器中,暴露的第一个解决方案(没有过滤器处理)可能会导致警告抑制。

更新:我认为更好,当我们只显示DeprecationWarning第一次为每个代码行,当我们可以发送一些消息:

import inspect
import traceback
import warnings
import functools

import time


def deprecated(message: str = ''):
    """
    This is a decorator which can be used to mark functions
    as deprecated. It will result in a warning being emitted
    when the function is used first time and filter is set for show DeprecationWarning.
    """
    def decorator_wrapper(func):
        @functools.wraps(func)
        def function_wrapper(*args, **kwargs):
            current_call_source = '|'.join(traceback.format_stack(inspect.currentframe()))
            if current_call_source not in function_wrapper.last_call_source:
                warnings.warn("Function {} is now deprecated! {}".format(func.__name__, message),
                              category=DeprecationWarning, stacklevel=2)
                function_wrapper.last_call_source.add(current_call_source)

            return func(*args, **kwargs)

        function_wrapper.last_call_source = set()

        return function_wrapper
    return decorator_wrapper


@deprecated('You must use my_func2!')
def my_func():
    time.sleep(.1)
    print('aaa')
    time.sleep(.1)


def my_func2():
    print('bbb')


warnings.simplefilter('always', DeprecationWarning)  # turn off filter
print('before cycle')
for i in range(5):
    my_func()
print('after cycle')
my_func()
my_func()
my_func()

结果:

before cycle
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:45: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
aaa
aaa
aaa
aaa
after cycle
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:47: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:48: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa
C:/Users/adr-0/OneDrive/Projects/Python/test/unit1.py:49: DeprecationWarning: Function my_func is now deprecated! You must use my_func2!
aaa

Process finished with exit code 0

我们可以点击警告路径,然后在PyCharm中找到这一行。

您可以创建一个utils文件

import warnings

def deprecated(message):
  def deprecated_decorator(func):
      def deprecated_func(*args, **kwargs):
          warnings.warn("{} is a deprecated function. {}".format(func.__name__, message),
                        category=DeprecationWarning,
                        stacklevel=2)
          warnings.simplefilter('default', DeprecationWarning)
          return func(*args, **kwargs)
      return deprecated_func
  return deprecated_decorator

然后导入已弃用的装饰器,如下所示:

from .utils import deprecated

@deprecated("Use method yyy instead")
def some_method():
 pass

正如muon所建议的,您可以为此安装弃用包。

弃用库为您的测试提供了一个已弃用的装饰器和一个fail_if_not_removed装饰器。

安装

pip install deprecation

示例使用

import deprecation

@deprecation.deprecated(deprecated_in="1.0", removed_in="2.0",
                        current_version=__version__,
                        details="Use the bar function instead")
def foo():
    """Do some stuff"""
    return 1

完整的文档请参见http://deprecation.readthedocs.io/。