相当于 python 中 mypy 的rankN 类型

问题描述 投票:0回答:1

在Haskell中,我们可以像这样使用rankN类型:

rankN :: (forall n. Num n => n -> n) -> (Int, Double)
rankN f = (f 1, f 1.0)

Python 中的 mypy 可以做同样的事情吗?

我在 python 3.10.2 和 mypy 1.7.1 中尝试了以下代码:

I = TypeVar("I", int, float)


def rankN(f: Callable[[I], I]) -> tuple[int, float]:
    return (f(1), f(1.0))

这会产生以下错误,这意味着

f
专门用于
float
:

Incompatible return value type (got "tuple[float, float]", expected "tuple[int, float]")  [return-value]
Argument 1 has incompatible type "float"; expected "int"  [arg-type]

我不一定期望这能起作用,因为 Haskell 情况下的魔法语法是嵌套的

forall
,但我不知道是否有类似的方法可以将其传达给 mypy(如果可能的话)。

python haskell types mypy
1个回答
0
投票

我不是Python专家,但从python/mypy#4395这样的问题来看,这似乎不存在,而且可能永远不会存在。

© www.soinside.com 2019 - 2024. All rights reserved.