Mypy 在继承的情况下输入 kwargs

Cle*_*ter 6 python inheritance mypy

我找到了几个关于如何处理kwargsMyPy 的答案。但实际上我的问题是 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, Union[int, str]]"; expected "str"
# Found 2 errors in 1 file (checked 1 source file)
Run Code Online (Sandbox Code Playgroud)

我不知道如何处理这个案子。我不想更新,Child.__init__因为它是一种不太可维护的模式。

miq*_*vir 3

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