【发布时间】:2019-06-04 16:44:52
【问题描述】:
假设我想使用 mypy 编写一个泛型类,但该类的类型参数本身就是一个泛型类型。例如:
from typing import TypeVar, Generic, Callable
A = TypeVar("A")
B = TypeVar("B")
T = TypeVar("T")
class FunctorInstance(Generic[T]):
def __init__(self, map: Callable[[Callable[[A], B], T[A]], T[B]]):
self._map = map
def map(self, x: T[A], f: Callable[[A], B]) -> T[B]:
return self._map(f, x)
当我尝试在上面的定义中调用 mypy 时出现错误:
$ mypy typeclasses.py
typeclasses.py:9: error: Type variable "T" used with arguments
typeclasses.py:12: error: Type variable "T" used with arguments
我尝试在T TypeVar 的定义中添加约束,但未能成功。可以这样做吗?
【问题讨论】:
标签: python type-hinting mypy higher-kinded-types python-typing