python - 如何在 mypy 中正确指定固定长度的序列?
问题描述
当我编写一个需要特定长度元组的函数时,我可以键入参数Tuple[int, int]
(对于特定长度为 2)。
from typing import Tuple
def tuple_in(a_tuple: Tuple[int, int]) -> Tuple[int, int]:
return a_tuple
tuple_in((0, 1)) # mypy is happy
只要我有一个元组要传递,那就太好了。如果我没有元组,我就很难将我拥有的任何东西都转换成一个元组。
tuple_in(tuple([0, 1])) # Expected Tuple[int, int], got Tuple[int, ...]
tuple_in(tuple(x for x in [0, 1])) # Expected Tuple[int, int], got Tuple[int, ...]
tuple_in(tuple(x for x in [0, 1][:2])) # Expected Tuple[int, int], got Tuple[int, ...]
我得到它。转换不确定长度的参数会产生一个长度不确定的元组。但这让我的生活变得困难。
这行得通,但它实际上不能使用超过 2-3 个值
my_list = [0, 1]
tuple_in((my_list[0], my_list[1])) # mypy is happy. My eyes hurt.
打字模块有一个cast
功能可以将它们Tuple[int, ...]
转换成Tuple[int, int]
,但这并不比type: ignore
.
tuple_in(cast(Tuple[int, int], "obviously not a tuple")) # mypy is happy
幸运的是,打字模块提供了一个更好的解决方案:NamedTuple
from typing import NamedTuple
TwoInts = NamedTuple("TwoInts", [("a", int), ("b", int)])
def named_tuple_in(a_tuple: TwoInts) -> Tuple[int, int]:
return a_tuple
named_tuple_in(Tuple2(*[0, 1])) # mypy is happy
但是,如果我想tuple_in
从模块外部调用,我必须 import TwoInts
。这似乎有点矫枉过正,这意味着我的编辑不会给我太多提示(只有我的 NamedTuple 的名称)。
当我的论点作为弱类有意义时(例如 Vector3、GenusSpecies、Address),我喜欢 NamedTuple 解决方案,但它感觉不是通用、固定长度参数(例如 TwoInts、FourteenScalars、ThreeNames)的最佳解决方案.
键入此类固定长度参数的预期方法是什么?
解决方案
我找到了!!
from typing import Tuple
def tuple_in(a_tuple: Tuple[int, int]) -> Tuple[int, int]:
return a_tuple
tuple_in([0, 1])[:2] # mypy is happy!!!
推荐阅读
- mediawiki - How to retrieve page's slug from Wikipedia API
- mysql - 查找 JOIN 两个表和 GROUP BY 收到的项目数量的总和
- pentaho - Previewing data from a step containing binary file is very slow in PDI
- r - Replace contents of a file and save file in directory - R
- rust - Why does a mutable iterator need a mutable reference when passed to a function?
- javascript - 如何从具有公共字段的三个数组中创建一个数组
- ssas - 具有硬编码成员的 SSAS 命名集
- dataframe - 在 Stata 中为多个成员资格多级模型准备数据
- flutter - 如何在颤振中创建六位唯一代码
- google-sheets - 表格公式帮助:如果列标题匹配,则将所有匹配值组合在一起(A、B、C,而不是 A+B+C)