微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在继承的情况下为 kwargs 输入 Mypy

如何解决在继承的情况下为 kwargs 输入 Mypy

我找到了几个关于如何使用 MyPy 处理 kwargs 的答案。但实际上我的问题是 mypy 没有正确地捕捉到这个:

from typing import Union


class Parent:
    def __init__(self,a: int,b: str) -> None:
        self.a = a
        self.b = b


class Child(Parent):
    def __init__(self,c: float,**kwargs: Union[int,str]) -> None:
        super().__init__(**kwargs)
        self.c = c


child = Child(a="a",b=2,c=2.3)

# tmp.py:12: error: Argument 1 to "__init__" of "Parent" has incompatible type "**Dict[str,Union[int,str]]"; expected "int"
# tmp.py:12: error: Argument 1 to "__init__" of "Parent" has incompatible type "**Dict[str,str]]"; expected "str"
# Found 2 errors in 1 file (checked 1 source file)

我不知道如何处理这种情况。我不想更新 Child.__init__,因为它是一种不太易于维护的模式。

解决方法

这是一个已知问题,已经有一段时间没有解决 (https://github.com/python/mypy/issues/1969)。

,

这似乎有效,但这只是一种解决方法吗?

from typing import overload

class Parent:

    def __init__(self,a: int,b: str) -> None:
        self.a = a
        self.b = b


class Child(Parent):

    @overload
    def __init__(self,c: float) -> None: ...

    @overload
    def __init__(self,c: float,*,a: int=...,b: str=...) -> None: ...

    def __init__(self,**kwargs) -> None:
        super().__init__(**kwargs)
        self.c = c


child = Child(2.3)
child = Child(2.3,a=3)
child = Child(2.3,b='5')
child = Child(2.3,a=3,b='5')
child = Child(a=3,b='5',c=2.3)
child = Child(a=3,b='5')  # mypy error ok
child = Child(a='3',c=2.3)  # mypy error ok
child = Child(a=3,b=5,c='2.3')  # mypy error ok

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。