gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:38:02 24 4
gpt4 key购买 nike

我找到了几个关于如何处理的答案 kwargs与 MyPy。但实际上我的问题是 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)
我不知道如何处理这个案子。我不想更新 Child.__init__因为它是一种不太易于维护的模式。

最佳答案

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

关于python - 在继承的情况下为 kwargs 输入 Mypy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66816116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com