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

Ste*_*ini 108 python decorator

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

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

Pat*_*oni 49

这里有一些片段,根据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
Run Code Online (Sandbox Code Playgroud)

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

  • 我不喜欢副作用(打开/关闭过滤器).决定这不是装饰者的工作. (14认同)
  • 为什么不使用`functools.wraps`而不是像那样设置名称和文档? (12认同)
  • 不回答实际问题。 (9认同)
  • 强烈同意@Kentzo - 禁用过滤器然后将它们重置为默认值会让一些开发人员非常头疼 (4认同)

Lau*_*RTE 34

这是另一个解决方案:

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

编辑:此代码使用Zero的建议:它替换warnings.warn_explicitline by 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)))
Run Code Online (Sandbox Code Playgroud)

您可以将此装饰器用于函数,方法.

这是一个简单的例子:

@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()
Run Code Online (Sandbox Code Playgroud)

你会得到:

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()
Run Code Online (Sandbox Code Playgroud)

EDIT3:这个装饰器现在是Deprecated库的一部分:

新的稳定版本v1.2.4

  • 工作,好吧 - 我更喜欢用`warnings.warn(msg,category = DeprecationWarning,stacklevel = 2)`替换`warn_explicit`行,它打印函数调用站点而不是函数定义站点.它使调试更容易. (5认同)
  • 不回答实际问题。 (4认同)
  • @DannyVarod 我知道,但对于代码来说,CC-BY-SA 比 GPL 更具限制性。当我问这个问题时,我正在开发一个 GPL 库。GPL 库可以使用 GPL 代码或更宽松的代码,但 GPL 库不能使用 CC-BY-SA 代码,因此我无法使用此代码片段。(无论如何,CC-BY-SA 从未用于代码;在更宽松的情况下,SO 在许可用户贡献中的代码片段方面会做得很好,因为现在,大多数用户无法使用他们在 SO 上找到的代码片段) (2认同)

jha*_*805 16

未来的 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: ...
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

来源:PEP702

  • @Catskul,也许这终于回答了问题? (3认同)

Ste*_*ica 9

如muon所建议,您可以deprecation为此安装软件包。

deprecation库为您的测试提供了一个deprecated装饰器和一个fail_if_not_removed装饰器。

安装

pip install deprecation
Run Code Online (Sandbox Code Playgroud)

用法示例

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
Run Code Online (Sandbox Code Playgroud)

有关完整文档,请参见http://deprecation.readthedocs.io/

  • 不回答实际问题。 (6认同)
  • 注意 *PyCharm* 无法识别这一点 (5认同)

ony*_*ony 8

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

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

  • 并且当从模块**导入函数时,您应该能够指示弃用**.装饰者将是一个正确的工具. (3认同)

Eri*_*uza 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
Run Code Online (Sandbox Code Playgroud)

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

from .utils import deprecated

@deprecated("Use method yyy instead")
def some_method()"
 pass
Run Code Online (Sandbox Code Playgroud)

  • 不回答实际问题。 (4认同)