当包装器具有实例变量时键入 class 装饰器

Typing for a class decorator when the wrapper has an instance variable

考虑到 mypy 的当前限制,这个装饰器的输入是否正确?我在下面包含示例用法:

import functools
from typing import TypeVar, Type, Any, cast

C = TypeVar('C', bound=Type[Any])


def singleton(cls: C) -> C:
    """Transforms a class into a Singleton (only one instance can exist)."""

    @functools.wraps(cls)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        if not wrapper.instance:  # type: ignore  # https://github.com/python/mypy/issues/2087
            wrapper.instance = cls(*args, **kwargs)  # type: ignore  # https://github.com/python/mypy/issues/2087
        return wrapper.instance  # type: ignore  # https://github.com/python/mypy/issues/2087

    wrapper.instance = None  # type: ignore  # https://github.com/python/mypy/issues/2087
    return cast(C, wrapper)


@singleton
class Test:
    pass


if __name__ == '__main__':
    a = Test()
    b = Test()
    print(a is b)

我必须在出现 instance 属性的行上添加 type: ignore,否则 mypy 会标记这些错误:

error: "Callable[..., Any]" has no attribute "instance"

您的函数接受类型为 C 的参数和类型为 returns 的结果。因此,根据 mypy ab 将具有相同的类型。您可以使用 reveal_type.

检查它
reveal_type(a) # Revealed type is 'test.Test'
reveal_type(b) # Revealed type is 'test.Test'

无论如何,cast# type: ignore 都应该谨慎使用,因为它们告诉 mypy 相信你(开发者)类型是正确的,即使它无法确认。

我在您的代码中看到的潜在问题是您将 class(即 Test)替换为一个函数,这可能会破坏一些代码。例如:

>>> Test
<function Test at 0x7f257dd2bae8>
>>> Test.mro()
AttributeError: 'function' object has no attribute 'mro'

您可以尝试的另一种方法是替换装饰 class:

__new__ 方法
def singleton(cls: C) -> C:
    """Transforms a class into a Singleton (only one instance can exist)."""

    new = cls.__new__

    def _singleton_new(cls, *args, **kwds):
        try:
            inst = cls._instance
        except AttributeError:
            cls._instance = inst = new(cls, *args, **kwds)
        return inst

    cls.__new__ = _singleton_new
    return cls

在这种情况下,您不会替换整个 class,因此您不太可能使用 class:

破坏其他代码
>>> Test                                                                                                                                                   
test.Test

>>> Test.mro()                                                                                                                                                           
[test.Test, object]

请注意,上面的代码只是一个示例,用于说明您当前方法的局限性。因此,您可能不应该按原样使用它,而应该寻找更可靠的解决方案。