先贴代码:
class AbstractMapper(ABC):
def __init__(self):
self._loaded_map: Dict["ObjectId", "DomainObject"] = {}
def _abstract_find(self, id_: "ObjectId") -> "DomainObject":
stmt = self.find_statement()
try:
rs = cursor.execute(stmt, (id_,)).fetchone()
res = self._load(rs)
return res
except Exception as e:
...
finally:
...
def _load(self, rs: tuple) -> "DomainObject":
if rs[0] in self._loaded_map:
return self._loaded_map[rs[0]]
res = self._do_load(rs)
self._loaded_map[res.id] = res
return res
@abstractmethod
def _do_load(self, rs: tuple) -> "DomainObject":
...
@abstractmethod
def find_statement(self) -> str:
...
class ArtistMapper(AbstractMapper):
def find_statement(self) -> str:
return "select id, name from artists where id = $1 limit 1"
def _do_load(self, rs: tuple) -> "DomainObject":
id_ = rs[0]
name = rs[1]
return Artist(id_, name)
def find(self, id_) -> "Artist":
return self._abstract_find(id_) # Expression of type "DomainObject" cannot be assigned to return type "Artist" "DomainObject" is incompatible with "Artist"
上面的代码中,_abstract_find()
的返回值标注为 DomainObject 类型,而在 ArtistMapper 中 find 方法需要返回一个 Artist 实例,Artist 是 DomainObject 的子类。
我看到 Java 代码中这里是通过强制类型转换解决的,Python 中这样的代码虽然可以正确运行,但无法通过静态类型检查,想知道有什么办法可以通过检查?
1
shyrock 2022-07-06 17:29:26 +08:00
Artist 跟 DomainObject 什么关系?
|
3
heqing 2022-07-06 18:20:50 +08:00 1
使用 Generic
|
4
hsfzxjy 2022-07-06 18:33:10 +08:00 via Android 1
粗暴点就用 typing.cast ,规范点就给 AbstractMapper 加范型参数
|
5
t2jk4000 2022-07-08 09:04:59 +08:00
|