如何为有界TypeVar和该TypeVar的迭代器创建泛型类?
赞:
Model = TypeVar('Model', bound=MODELS_BASE_CLASS)
RecordReaderType = TypeVar('RecordReaderType', Model, Iterator[Model])
class RecordReader(Generic[RecordReaderType]):
def func(self) -> RecordReaderType:
pass
mypy引发错误:类型变量Model未绑定。
有关MODELS_BASE_CLASS的更新:这是一个导入的类,广泛用作项目类的基础。像这样:
from lib import BaseClass
class FirstSubClass(BaseClass):
pass
class SecondSubClass(BaseClass):
pass
Model = TypeVar('Model', bound=BaseClass)