我有一个数据类,如下所示
@dataclass
class DataObj:
""" DataObj creates an object for each record in data
"""
name: str
profit: int
space: float
我有另一个容器类,它保存上述类的对象
class DataContainer:
""" Data Container class holding objects of DataObj class
"""
def __init__(self, data_tuple_list: List[Tuple[str, int, float]]) -> None:
self.container = []
for data_tuple in data_tuple_list:
self.container.append(DataObj(*data_tuple))
def __getitem__(self, n: int) -> Type[DataObj]:
return self.container.__getitem__(n)
然而,当我使用mypy判断代码时,我得到了以下错误
error: Incompatible return value type (got "DataObj", expected "Type[DataObj]")
Found 1 error in 1 file (checked 1 source file)
我的python版本是3.8.10
.
问题:如何修复错误.
编辑
- 如果我用
self.container: List[DataObj] = []
,我得到error: Incompatible return value type (got "DataObj", expected "Type[DataObj]") Found 1 error in 1 file (checked 1 source file)
- 如果我用
self.container: List[Type[DataObj]] = []
,我得到error: Argument 1 to "append" of "list" has incompatible type "DataObj"; expected "Type[DataObj]" Found 1 error in 1 file (checked 1 source file)
.