检查 stdlib 函数时出现 Mypy 错误?

Mypy error when checking stdlib functions?

我有一个例程来创建一个有效的动力组。我在参数和 return 值中添加了一些类型注释,然后在结果上 运行 Mypy。

Mypy 似乎对 stdlib 函数有问题,这是预料之中的吗?

(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$ python -V
Python 3.8.5
(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$ mypy -V
mypy 0.761
(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$ uname -a
Linux Paddy-G14 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$ cat pwrset_mypy.py
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 30 14:59:19 2021

@author: Paddy3118
"""
from itertools import chain, combinations
from typing import List, Tuple


def powerset(s: List[int]) -> List[Tuple[int]]:
    """powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3) ."""
    return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))

if __name__ == '__main__':
    assert powerset([0, 1, 2]) == [(), (0,), (1,), (2,), (0, 1), (0, 2),
                                   (1, 2), (0, 1, 2)]
(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$
(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$ mypy  --show-error-context --show-column-numbers --show-error-codes --pretty pwrset_mypy.py
pwrset_mypy.py: note: In function "powerset":
pwrset_mypy.py:13:37: error: Generator has incompatible item type "Iterator[Tuple[int, ...]]"; expected
"Iterable[Tuple[int]]"  [misc]
        return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))
                                        ^
Found 1 error in 1 file (checked 1 source file)
(base) paddy3118@Paddy-G14:/mnt/c/Users/paddy/Google Drive/Code$
``

您收到错误消息是因为您的类型注释有误。错误消息告诉您确切的修复方法——您可以使用 Tuple[int, ...] 而不是 Tuple[int]

def powerset(s: List[int]) -> List[Tuple[int, ...]]:
    """powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3) ."""
    return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))

除非调用者明确知道这些是元组很重要,否则另一种选择是将它们指定为 Iterable[int]s:

def powerset(s: List[int]) -> List[Iterable[int]]:
    """powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3) ."""
    return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))

请注意,Tuple[int] 是一个包含单个 int 的元组,而 Iterable[int] 是包含任意数量 int 的任何可迭代对象。元组类型声明为元组的每个位置采用一个类型参数——因为每个单独的元组都是不可变的,所以可以注释每个位置并知道类型将被保留,所以你可以有例如 Tuple[int, int] 或一个Tuple[int, str],等等