简体   繁体   English

如何使用类型注释来指示未知长度的Tuple类型参数是返回的映射的关键?

[英]How do I use type annotations to indicate an unknown-length Tuple type parameter is the key of the returned Mapping?

Say I've got a function like this: 说我有一个这样的功能:

def a_function(data: Tuple[Tuple[str, ...], ...]) -> Mapping[Tuple[str, ...], int]:
    pass

Is there a way to indicate via type annotations that the Tuple[str, ...] is going to be the same Tuple[str, ...] that is passed in via the data parameter? 有没有一种方法可以通过类型注释指示Tuple[str, ...]将与通过data参数传入的Tuple[str, ...]相同?

In other words, the tuple being passed in will contain a homogenous list of homogenous tuples that all might look like ('fred', 'smith'), ('ananya', 'cooper') and the returned dict will look like {('fred', 'smith'): 7, ('ananya', 'cooper'): 13} . 换句话说,传入的元组将包含一个同质元组的同源列表,这些元组可能看起来像('fred', 'smith'), ('ananya', 'cooper') ,返回的dict看起来像{('fred', 'smith'): 7, ('ananya', 'cooper'): 13}

I don't know if that's what would satisfy you, but you could do something like this: 我不知道这是否会让您满意,但是您可以执行以下操作:

SomeType = TypeVar('SomeType', Tuple[str, ...])


def a_function(data: Tuple[SomeType, ...]) -> Mapping[SomeType, int]:
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM