找到正确的 Python 类型提示,例如,内置函数 map() 的签名

Finding the proper Python type hint, for instance, the signature of the built-in function map()

描述

在 Python 3.5 或更高版本中,支持类型提示(有关详细信息,请参阅 here)。但是,常见类型的正确用法并没有很好的记录。

例如,从官方网站,我可以收集到以下(正确的)用法:

字符串

# The function signature of greeting is: Callable[[str], str]
def greeting(name: str) -> str:
    return 'Hello ' + name

整数

# The function signature of add is: Callable[[int, int], int]
def add(a: int, b: int) -> int:
    return a + b

浮点数

# The default value of x is 1.0
def reciprocal(x: float = 1.0) -> float:
    from math import nan
    if x == 0:
        return nan
    return 1 / x

列表

from typing import List, TypeVar

T = TypeVar('T')

def repeat(x: T, n: int) -> List[T]:
    return [x] * n

元组

from typing import Tuple, TypeVar

T = TypeVar('T')

def double(x: T) -> Tuple[T, T]:
    return (x, x)

问题

我的问题是:

1. map的return类型是什么?

from typing import Iterable

# Is this correct?
x: Iterable[float] = map(int.__float__, [1, 2, 3])

我不确定这是否是上面 x 的正确类型提示。

2。广义上,map的'function signature'是什么?

from typing import Callable, Iterable, TypeVar

T = TypeVar('T')
U = TypeVar('U')

# In the above usage, the type of the map function seems to be:
Function1 = Callable[[T], U]
typeOfMap = Callable[[Function1, Iterable[T]], Iterable[U]]

# Or in one line:
typeOfMap = Callable[[Callable[[T], U], Iterable[T]], Iterable[U]]

但实际上map函数是可以接受多个iterables的。它是 documented 作为:

map(function, iterable, ...)

可以这样使用:

# The result is: [['A'], ['B', 'B'], ['C', 'C', 'C']]
result = list(map(repeat, ['A', 'B', 'C'], [1, 2, 3]))

T1 = TypeVar('T1')
T2 = TypeVar('T2')
R = TypeVar('R')

# So, the type of map function in this usage seems to be:
Function2 = Callable[[T1, T2], R]
typeOfMap = Callable[[Function2, Iterable[T1], Iterable[T2]], Iterable[R]]

总的来说,我猜应该是这样的,但是这样写是不正确的:

FunctionN = Callable[[T1, T2, ..., Tn], R]
typeOfMap = Callable[[FunctionN, Iterable[T1], Iterable[T2], ..., Iterable[Tn]], Iterable[R]]

那么,正确的写法是什么?

3。一般来说,我在哪里可以找到 Python 函数/方法的正确类型提示,包括内置的,核心库中的那些?

我需要它们主要是为了学习。

4.有没有办法输出编译器/解释器计算的类型推断结果,如Haskell或Erlang?

我知道 Haskell 和 Erlang 是函数式编程语言,变量是不可变的,所以实现这一点会容易得多,但以防万一 Python 也有类似的功能,我想知道。

5.有什么方法可以检查我的类型提示是否正确?

或者至少让它在编译时/运行时向我显示一些警告/错误,以便我知道出了什么问题。

参考资料

在撰写本文时,最新的稳定版本是 3.8.0。

1. What is the return type of map?

map 的 return 值 is currently typed as an Iterator[T]。由于迭代器是可迭代的,因此 x: Iterable[float] = map(int.__float__, [1, 2, 3]) 的注释是有效的。 (另外,只写map(float, [1, 2, 3])。)

2. In a broader sense, what is the 'function signature' of map?

目前无法表示,所以typeshed has overloads for up to 6 generic parameters. (one related issue)

3. In general, where can I find the correct type hint of a Python function / method, including the built-in ones, those in the core libraries?

I need them mainly for learning purpose.

Python 的 typeshed repository。请注意,对于 学习目的——练习——你通常应该让类型被推断或使用任何有意义的类型而不是尽可能准确的类型。

您也可以使用 reveal_type – 见下文。

4. Is there any way to output the type inference result computed by the compiler / interpreter, as in Haskell or Erlang?

I know Haskell and Erlang are functional programming languages and variables are immutable, so it would be much easier to make this possible, but just in case if Python also has similar functionalities, I would like to know.

这取决于您的类型检查器。 Mypy 有 reveal_type.

x = map(float, [1, 2, 3])
reveal_type(x)  # note: Revealed type is 'typing.Iterator[builtins.float*]'

5. Is there any way to check that my type hint is correct?

Or at least let it show me some warning / error at compile time / runtime so that I am aware that something is wrong.

从静态上讲,这就是类型检查。 (例如,如果您的 x 提示错误,Mypy 会告诉您。)它无法捕获所有内容,尤其是在像 Python 这样动态的语言中,但这就是编程的本质。

有一些项目基于类型注释进行某种程度的运行时断言,但是none我已经使用过或者会很费心。