当函数的期望参数类型为paste()
并且我传入incompatible type error
时,MyPy给了我Dict[Union[CustomObject1, CustomObject2], ...]
。
请考虑以下内容:
Dict[CustomObject1, ...]
错误是:from typing import Dict, List, Union
import dataclasses
@dataclasses.dataclass(frozen=True)
class MyKey:
myStr: str
@dataclasses.dataclass(frozen=True)
class MySecondKey:
myStr: str
@dataclasses.dataclass
class CustomDataObject:
dataProp: Dict[MySecondKey, Dict[str, List[Dict[str, int]]]]
sample_data = {MySecondKey(myStr="a"): {"foo": [{"bar": 0}]}}
sample_prop = CustomDataObject(dataProp=sample_data).dataProp
def dummy_func(
some_data: Dict[
Union[MyKey, MySecondKey], Dict[str, List[Dict[str, int]]]
],
):
print(some_data)
print(dummy_func(sample_prop))
。
为什么会这样?我该如何解决这个问题?