繁体   English   中英

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM