我正在尝试用这样的泛型类型编写一些代码:
from typing import Sequence, TypeVar, Hashable, Protocol, NoReturn
class _SortHashable(Protocol, Hashable):
def __lt__(self, other) -> bool:
...
def __eq__(self, other) -> bool:
...
SortHashableT = TypeVar("SortHashableT", bound=_SortHashable)
def foo(sequence: Sequence[SortHashableT]) -> NoReturn:
...
对于
other
对象的类型提示,我尝试了以下操作:
class _SortHashable(Protocol, Hashable):
def __lt__(self, other: _SortHashable) -> bool:
...
但我想这不受支持,我的编辑将其标记为错误。有没有一种方法可以像 C++ 等语言那样转发声明类型?我在这个项目中使用 Python 3.10。