使用多种键值类型输入Dict

问题描述

我有一个python函数,该函数返回具有以下结构的字典

{
   (int,int): {string: {string: int,string: float}}
}

我想知道如何使用类型提示来指定它。因此,这些位很清楚:

Dict[Tuple[int,int],Dict[str,# what comes here]]

但是,内部字典的两个键具有intfloat值类型。我不确定该如何注释

解决方法

您应该可以使用Union

联盟类型; Union[X,Y]表示X或Y。

from typing import Union

Dict[Tuple[int,int],Dict[str,Union[int,float]]]

话虽如此,如果键始终相同,最好使用tuplenamedtuple代替内部dict