3

尝试使用 mypy 检查以下代码时:

import itertools
from typing import Sequence, Union, List
        
DigitsSequence = Union[str, Sequence[Union[str, int]]]


def normalize_input(digits: DigitsSequence) -> List[str]:
    try:
        new_digits = list(map(str, digits))  # <- Line 17
        if not all(map(str.isdecimal, new_digits)):
            raise TypeError
    except TypeError:
        print("Digits must be an iterable containing strings.")
        return []
    return new_digits

mypy 抛出以下错误:

calculate.py:17:错误:无法推断“地图”的类型参数 1

为什么会出现这个错误?我该如何解决?

谢谢 :)

编辑:这实际上是mypy 中的一个错误,现在已修复。

4

1 回答 1

3

您可能已经知道,mypy依赖于typeshed来存根 Python 标准库中的类和函数。我相信您的问题与 typeshed 的存根有关map

@overload
def map(func: Callable[[_T1], _S], iter1: Iterable[_T1]) -> Iterator[_S]: ...

并且 mypy 的当前状态使得它的类型推断不是无限的。(该项目还有600 多个未解决的问题。

我相信您的问题可能与问题 #1855有关。我相信情况确实如此,因为DigitsSequence = str两者DigitsSequence = Sequence[int]都有效,而DigitsSequence = Union[str, Sequence[int]]无效。

一些解决方法:

  1. 改用 lambda 表达式:

    new_digits = list(map(lambda s: str(s), digits))
    
  2. 重新转换为新变量:

    any_digits = digits # type: Any
    new_digits = list(map(str, any_digits))
    
  3. 要求 mypy 忽略该行:

    new_digits = list(map(str, digits)) # type: ignore
    
于 2017-07-08T20:34:53.493 回答