python - 如何警告类(名称)弃用

标签 python class backwards-compatibility deprecation-warning

我已经重命名了作为库一部分的 python 类。我愿意在一段时间内保留使用它以前的名称的可能性,但想警告用户它已被弃用,将来会被删除。

我认为为了提供向后兼容性,使用这样的别名就足够了:

class NewClsName:
    pass

OldClsName = NewClsName

我不知道如何以优雅的方式将 OldClsName 标记为已弃用。也许我可以使 OldClsName 成为一个函数,它会发出警告(对日志)并从其参数构造 NewClsName 对象(使用 *args**kvargs) 但它看起来不够优雅(或者可能是?)。

但是,我不知道 Python 标准库弃用警告是如何工作的。我想可能有一些很好的魔法来处理弃用,例如允许根据某些解释器的命令行选项将其视为错误或静音。

问题是:如何警告用户使用过时的类别名(或一般的过时类)。

编辑:函数方法对我不起作用(我已经尝试过),因为该类有一些类方法(工厂方法)在 OldClsName 被定义为一个函数。以下代码不起作用:

class NewClsName(object):
    @classmethod
    def CreateVariant1( cls, ... ):
        pass

    @classmethod
    def CreateVariant2( cls, ... ):
        pass

def OldClsName(*args, **kwargs):
    warnings.warn("The 'OldClsName' class was renamed [...]",
                  DeprecationWarning )
    return NewClsName(*args, **kwargs)

OldClsName.CreateVariant1( ... )

因为:

AttributeError: 'function' object has no attribute 'CreateVariant1'

继承是我唯一的选择吗?老实说,它在我看来不是很干净——它通过引入不必要的派生来影响类层次结构。此外,OldClsName is not NewClsName 在大多数情况下这不是问题,但在使用库编写的代码写得不好时可能会出现问题。

我还可以创建一个虚拟的、不相关的 OldClsName 类,并为其中的所有类方法实现构造函数和包装器,但在我看来,这是更糟糕的解决方案。

最佳答案

Maybe I could make OldClsName a function which emits a warning (to logs) and constructs the NewClsName object from its parameters (using *args and **kvargs) but it doesn't seem elegant enough (or maybe it is?).

是的,我认为这是非常标准的做法:

def OldClsName(*args, **kwargs):
    from warnings import warn
    warn("get with the program!")
    return NewClsName(*args, **kwargs)

唯一棘手的事情是,如果您有从 OldClsName 子类化的东西 - 那么我们必须变得聪明。如果您只需要保持对类方法的访问权限,应该这样做:

class DeprecationHelper(object):
    def __init__(self, new_target):
        self.new_target = new_target

    def _warn(self):
        from warnings import warn
        warn("Get with the program!")

    def __call__(self, *args, **kwargs):
        self._warn()
        return self.new_target(*args, **kwargs)

    def __getattr__(self, attr):
        self._warn()
        return getattr(self.new_target, attr)

OldClsName = DeprecationHelper(NewClsName)

我还没有测试过,但这应该会给你一个想法 - __call__ 将处理正常实例化路由,__getattr__ 将捕获对类方法的访问 &仍然会生成警告,而不会弄乱您的类层次结构。

关于python - 如何警告类(名称)弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9008444/

相关文章:

html - 如何在 <IE6 旧版浏览器中仅显示 HTML

javascript - 在 javascript1.3 之前使用 === 会产生什么后果?

python - 恢复标准化操作时的精度问题

c++ - 在 C++ 中通过引用将地址传递给类并从那里传递给它的 "child-class"

c++ - C++ 中的类方法等价物

ios - Swift 3 - 将 bool 值发送到不同的类

Java 处理两个自定义反序列化器

java - Python 和 Java 中的矩阵减法差异

python - 最佳替代数据结构以改善由于字典大小而导致的运行时间?

python - 使用python misses服务浏览avahi服务