问题描述
我找到了几个关于如何使用 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