在 Python 中,如何在使用 class 装饰器时正确输入?

In Python, how to get typing right when using a class decorator?

这是我第一次尝试制作 Python 装饰器,我谦虚地承认在这样做的过程中从许多 Whosebug 帖子中汲取了灵感。当我清除 __instancecheck__ 方法时,最后两个断言停止触发,但现在 PyCharm 的内置 linter 对最后一个断言不满意。

Expected type 'Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]', got 'Multiton' instead.

打字问题的根源是什么,如何解决?

添加, 在进行评论者推荐的 __init__ 更改后另一个警告表面。

Local variable 'instance' might be referenced before assignment.
class Multiton(object):
    """ When init parameters match, reuse an old initialized object instead of making a new one. """
    def __init__(self, cls):
        # self.__dict__.update({'instances': list(), 'cls': cls})
        self.instances = list()
        self.cls = cls

    def __call__(self, *args, **kwargs):
        # make a key with the parameters
        key = (args, kwargs)

        # search for a matching old instance
        found = False
        for instance in self.instances:
            if instance['key'] == key:
                found = True
                break

        # if not found then create a new instance
        if not found:

            instance = {'key': key, 'object': self.cls(*args, **kwargs)}
            self.instances.append(instance)

        return instance['object']

    def __instancecheck__(self, other):
        return isinstance(other, self.cls)


@Multiton
class YourClass:

    def __init__(self, number):
        self.number = number


yc_1 = YourClass(1)
yc_2 = YourClass(2)
yc_two = YourClass(2)

assert yc_1 != yc_2
assert yc_2 == yc_two

assert not isinstance(yc_1, Multiton)
assert isinstance(yc_1, YourClass)

错误警告是 PyCharm 错误,希望 JetBrains 的优秀人员尽快修复。

https://youtrack.jetbrains.com/issue/PY-38590 https://youtrack.jetbrains.com/issue/PY-49966