繁体   English   中英

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

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

描述

在 Python 3.5 或更高版本中,支持类型提示(有关更多信息,请参见此处)。 但是,常见类型的正确用法没有得到很好的记录。

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

细绳

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

Integer

# 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的返回类型是什么?

from typing import Iterable

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

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

2. 从广义上讲, map的“功能签名”是什么?

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 function 可以接受多个迭代。 它被记录为:

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 function / 方法的正确类型提示,包括内置的,核心库中的那些?

我需要它们主要用于学习目的。

4. 有什么办法可以得到output编译器/解释器计算的类型推断结果,如Haskell或Erlang?

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

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

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

参考

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

1. map的返回类型是什么?

map的返回值当前类型为Iterator[T] 由于迭代器是可迭代的,因此您对x: Iterable[float] = map(int.__float__, [1, 2, 3])的注释是有效的。 (另外,只需写map(float, [1, 2, 3]) 。)

2、从更广泛的意义上来说,map的‘函数签名’是什么?

目前无法表示,这就是为什么typeshed 具有最多 6 个泛型参数的重载 一个相关问题

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

我需要它们主要用于学习目的。

Python 的类型库 请注意,出于学习目的(练习),您通常应该让类型被推断或使用任何有意义的类型,而不是最精确的类型。

你也可以使用reveal_type见下文。

4. 有什么办法可以得到output编译器/解释器计算的类型推断结果,如Haskell或Erlang?

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

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

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

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

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

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

有一些项目基于类型注释执行某种级别的运行时断言,但我没有使用过或真正会费心。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM