python标准库中的装饰器(@deprecated特指)

fnatzsnv  于 2023-01-24  发布在  Python
关注(0)|答案(7)|浏览(155)

我需要将例程标记为deprecated,但显然没有用于deprecated的标准库装饰器。我知道它的配方和警告模块,但我的问题是:为什么没有标准的库装饰器来完成这个任务?
补充问题:在标准库中有标准的装饰器吗?

ux6nzvsh

ux6nzvsh1#

这里有一些片段,修改从那些引用莱安德罗:

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

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

63lcw9qa

63lcw9qa2#

下面是另一个解决方案:
这个decorator(实际上是decorator factory)允许你给出一个reason消息,通过给出源代码filenameline number来帮助开发者诊断问题也是非常有用的。

    • EDIT**:此代码使用Zero的建议:将warnings.warn_explicit行替换为warnings.warn(msg, category=DeprecationWarning, stacklevel=2),打印函数调用位置而不是函数定义位置,使调试更容易。
    • EDIT2**:此版本允许开发人员指定可选的"原因"消息。
import functools
import inspect
import warnings

string_types = (type(b''), type(u''))

def deprecated(reason):
    """
    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.
    """

    if isinstance(reason, string_types):

        # The @deprecated is used with a 'reason'.
        #
        # .. code-block:: python
        #
        #    @deprecated("please, use another function")
        #    def old_function(x, y):
        #      pass

        def decorator(func1):

            if inspect.isclass(func1):
                fmt1 = "Call to deprecated class {name} ({reason})."
            else:
                fmt1 = "Call to deprecated function {name} ({reason})."

            @functools.wraps(func1)
            def new_func1(*args, **kwargs):
                warnings.simplefilter('always', DeprecationWarning)
                warnings.warn(
                    fmt1.format(name=func1.__name__, reason=reason),
                    category=DeprecationWarning,
                    stacklevel=2
                )
                warnings.simplefilter('default', DeprecationWarning)
                return func1(*args, **kwargs)

            return new_func1

        return decorator

    elif inspect.isclass(reason) or inspect.isfunction(reason):

        # The @deprecated is used without any 'reason'.
        #
        # .. code-block:: python
        #
        #    @deprecated
        #    def old_function(x, y):
        #      pass

        func2 = reason

        if inspect.isclass(func2):
            fmt2 = "Call to deprecated class {name}."
        else:
            fmt2 = "Call to deprecated function {name}."

        @functools.wraps(func2)
        def new_func2(*args, **kwargs):
            warnings.simplefilter('always', DeprecationWarning)
            warnings.warn(
                fmt2.format(name=func2.__name__),
                category=DeprecationWarning,
                stacklevel=2
            )
            warnings.simplefilter('default', DeprecationWarning)
            return func2(*args, **kwargs)

        return new_func2

    else:
        raise TypeError(repr(type(reason)))

您可以将这个装饰器用于函数方法
下面是一个简单的例子:

@deprecated("use another function")
def some_old_function(x, y):
    return x + y

class SomeClass(object):
    @deprecated("use another method")
    def some_old_method(self, x, y):
        return x + y

@deprecated("use another class")
class SomeOldClass(object):
    pass

some_old_function(5, 3)
SomeClass().some_old_method(8, 9)
SomeOldClass()

您将获得:

deprecated_example.py:59: DeprecationWarning: Call to deprecated function or method some_old_function (use another function).
  some_old_function(5, 3)
deprecated_example.py:60: DeprecationWarning: Call to deprecated function or method some_old_method (use another method).
  SomeClass().some_old_method(8, 9)
deprecated_example.py:61: DeprecationWarning: Call to deprecated class SomeOldClass (use another class).
  SomeOldClass()

新稳定版本v1.2.13🎉

yptwkmov

yptwkmov3#

正如muon所建议的,您可以为此安装deprecation包。
deprecation库为您的测试提供了一个deprecated装饰器和一个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/

ct2axkht

ct2axkht4#

我猜原因是Python代码不能静态处理(就像C++编译器那样),你不能在实际使用它之前得到警告。我不认为向脚本用户发送一堆“警告:此脚本的开发人员正在使用已弃用的API”“。

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

vh0rcniy

vh0rcniy5#

您可以创建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
vkc1a9a2

vkc1a9a26#

Python是一种动态类型语言。对于静态函数,不必将类型声明为变量或参数类型。
因为它是动态的,如果在运行时处理的话,任何事情都是动态的。即使一个方法被弃用,它也只能在运行时或解释过程中被知道。
使用deprecation模块来弃用方法。
deprecation是一个支持自动化弃用的库,它提供了deprecated()装饰器来 Package 函数,在文档中和Python的warnings系统中提供适当的警告,还提供了**deprecation. fail_if_not_removed()**装饰器,用于测试方法,以确保弃用的代码最终被移除。

安装:

python3.10 -m pip install deprecation

小型演示:

import deprecation

@deprecation.deprecated(details="Use bar instead")
def foo():
    print("Foo")

def bar():
    print("Bar")

foo()

bar()

输出:

test.py: DeprecatedWarning: foo is deprecated. Use bar instead
  foo()

Foo

Bar
j0pj023g

j0pj023g7#

更新:我认为更好,当我们只对每个代码行第一次显示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中的行。

相关问题