我想将TypeVar角色限制为Hashable。例如,
def convert_to_set(x: List[T]) -> Set[T]:
return set(x)我想指出T是Hashable的子类,因为set的所有元素都必须是hashable的。
我认为其中一个解决方案是:
TH = TypeVar("TH", Hashable, Hashable)然而,我认为它是丑陋的。
我该怎么办?
发布于 2020-04-22 12:56:05
我从https://mypy.readthedocs.io/en/stable/generics.html#type-variables-with-upper-bounds那里得到了一个解决方案
T = TypeVar(T, bound=Hashable)https://stackoverflow.com/questions/56106221
复制相似问题