【发布时间】:2020-09-10 12:27:36
【问题描述】:
像 numpy.intersect1d 这样的一些函数返回不同的类型(在这种情况下是一个 ndarray 或三个 ndarray 的元组)但编译器只能推断其中一个,所以如果我愿意:
intersection: np.ndarray = np.intersect1d([1, 2, 3], [5, 6, 2])
它会引发类型警告:
Expected type 'ndarray', got 'Tuple[ndarray, ndarray, ndarray]' instead
我可以在 Typescript 等其他语言中避免此类问题,我可以在其中使用 as 关键字到 assert the type(在运行时没有影响)。我已经阅读了文档并看到了 cast 函数,但我想知道是否有任何 inline 解决方案或我缺少的东西。
【问题讨论】:
-
mypy.readthedocs.io/en/stable/casts.html - 由于文档没有提到任何替代方法来进行类型断言,我认为没有。
-
感谢您的评论!如果您喜欢回答,我会标记为已接受
标签: python python-3.x mypy