Python 标准库中的装饰器(具体@deprecated)

decorators in the python standard lib (@deprecated specifically)

提问人:Stefano Borini 提问时间:3/29/2010 最后编辑:Ken WilliamsStefano Borini 更新时间:11/17/2023 访问量:152939

问:

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

附加问题:标准库中是否有标准装饰器?

Python 装饰器 已弃用

评论

23赞 muon 11/2/2017
现在有一个弃用
19赞 SwimBikeRun 4/1/2019
我了解这样做的方法,但来这里是为了了解为什么它不在 std 库中(我认为是 OP 的情况),并且没有看到实际问题的良好答案
20赞 Catskul 5/3/2019
为什么经常发生这样的情况,以至于问题得到几十个答案,甚至没有试图回答问题,并主动忽略诸如“我知道食谱”之类的事情?这太疯狂了!
7赞 Stefano Borini 5/3/2019
@Catskul是因为假的互联网积分。
2赞 Wolf 5/27/2021
@Catskul 为什么问题根本不被弃用,或者它们不应该出现在 SO 上吗?正如我们经常看到的那样,它们往往会产生无答案。如果我没有看到你所有的不回答装饰器😉,我就不会留下这个评论

答:

19赞 ony 3/29/2010 #1

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

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

评论

5赞 Janusz Lenar 2/21/2013
并且,当从模块导入函数时,您应该能够指示弃用。Decorator 将是一个合适的工具。
0赞 ony 2/21/2013
@JanuszLenar,即使我们不使用该已弃用的函数,也会显示该警告。但我想我可以用一些提示来更新我的答案。
92赞 Patrizio Bertoni 5/15/2015 #2

以下是一些片段,根据 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

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

评论

14赞 Maximilian 8/6/2015
为什么不使用而不是像那样设置名称和文档呢?functools.wraps
1赞 Eric 6/28/2016
@Maximilian:编辑以添加它,以防止将来复制粘贴此代码的人也做错
33赞 Kentzo 4/19/2017
我不喜欢副作用(打开/关闭过滤器)。决定这一点不是装饰师的工作。
23赞 Catskul 5/3/2019
没有回答实际问题。
4赞 Aaron V 12/8/2020
非常同意@Kentzo - 禁用过滤器然后将它们重置为默认值会让一些开发人员非常头疼
82赞 Laurent LAPORTE 10/28/2016 #3

这是另一种解决方案:

这个装饰器(实际上是一个装饰器工厂)允许你给出一个原因信息。通过提供源文件名行号来帮助开发人员诊断问题也更有用。

编辑:此代码使用 Zero 的建议:它将行替换为 , 打印函数调用站点,而不是函数定义站点。它使调试更容易。warnings.warn_explicitwarnings.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()

编辑3:此装饰器现在是已弃用库的一部分:

新的稳定版本 v1.2.13 🎉

评论

6赞 Zero 11/29/2016
工作得很好 - 我更喜欢替换打印函数调用站点而不是函数定义站点的行。它使调试更容易。warn_explicitwarnings.warn(msg, category=DeprecationWarning, stacklevel=2)
0赞 gerrit 7/8/2017
您好,我想在 GPLv3 许可的库中使用您的代码片段。你是否愿意在 GPLv3 或任何更宽松的许可证下重新授权你的代码,以便我可以合法地这样做?
1赞 gerrit 7/9/2017
@LaurentLAPORTE我知道。CC-BY-SO 不允许在 GPLv3 中使用(因为相同方式共享),这就是为什么我问你是否愿意在 GPL 兼容的许可证下专门发布这段代码。如果没有,那也没关系,我不会使用你的代码。
13赞 Catskul 5/3/2019
没有回答实际问题。
2赞 gerrit 5/10/2021
@DannyVarod我知道,但对于代码来说,CC-BY-SA 比 GPL 更严格。当我问这个问题时,我正在开发一个 GPL 库。GPL 库可以使用 GPL 代码或更宽松的代码,但 GPL 库不能使用 CC-BY-SA 代码,所以我无法使用这个代码片段。(无论如何,CC-BY-SA 从未用于代码;SO 在更宽松的情况下许可用户贡献中的代码片段会很好,因为就像现在一样,大多数用户不能使用他们在 SO 上找到的代码片段)
0赞 ADR 12/1/2016 #4

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

评论

8赞 Catskul 5/3/2019
没有回答实际问题。
19赞 Erika Dsouza 2/6/2018 #5

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

评论

1赞 German Attanasio 2/6/2018
谢谢,我使用它来将用户发送到正确的地方,而不仅仅是显示弃用消息!
13赞 Catskul 5/3/2019
没有回答实际问题。
32赞 Stevoisiak 5/17/2018 #6

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

该库为测试提供修饰器和修饰器。deprecationdeprecatedfail_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/

评论

16赞 Catskul 5/3/2019
没有回答实际问题。
6赞 c z 8/23/2019
注意:PyCharm 无法识别这一点
1赞 Udesh 2/21/2022 #7

Python是一种动态类型语言。没有必要静态地将类型声明为函数的变量或参数类型。

由于它是动态的,所以在运行时处理的每一件事。即使某个方法被弃用,它也只会在运行时或解释期间被知道。

使用用模块弃用方法。

弃用是一个支持自动弃用的库。它 提供 deprecated() 装饰器来包装函数,提供适当的 文档中的警告和通过 Python 的警告系统,作为 以及用于测试的 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

评论

0赞 bfontaine 10/20/2023
Python 是一种动态类型语言,但它确实通过模块进行静态类型化。此类型在运行时不起作用;它仅供静态分析工具(如或编辑器集成)使用。我对这个答案投反对票,因为它提出了一个在问题正文中明确提到不令人满意的解决方案。运行时警告有效,但它们不会告诉 mypy 或您的编辑器此功能已弃用。typingmypy
7赞 thehale 6/20/2023 #8

未来的 Python 版本(3.12 之后)将包括 warnings.deprecated 装饰器,它将指示弃用类型检查器,例如 .mypy

例如,请考虑以下名为 library.pyi 的库存根:

from warnings import deprecated

@deprecated("Use Spam instead")
class Ham: ...

@deprecated("It is pining for the fiords")
def norwegian_blue(x: int) -> int: ...

@overload
@deprecated("Only str will be allowed")
def foo(x: int) -> str: ...
@overload
def foo(x: str) -> str: ...

以下是类型检查器应如何处理此库的使用:

from library import Ham  # error: Use of deprecated class Ham. Use Spam instead.

import library

library.norwegian_blue(1)  # error: Use of deprecated function norwegian_blue. It is pining for the fiords.
map(library.norwegian_blue, [1, 2, 3])  # error: Use of deprecated function norwegian_blue. It is pining for the fiords.

library.foo(1)  # error: Use of deprecated overload for foo. Only str will be allowed.
library.foo("x")  # no error

ham = Ham()  # no error (already reported above)

来源:PEP702

评论

1赞 CervEd 8/11/2023
@Catskul,也许这最终回答了这个问题?