假设我有这些课程:
class GenericCopyable:
def copy(self) -> GenericCopyable:
... # whatever is required to copy this
class CopyableFoo(GenericCopyable):
... # uses the parent implementation of "copy"
def bar(self): …
def some_code(victim: CopyableFoo):
v = victim.copy()
v.bar() ### I know that this works because "v" is a CopyableFoo, but mypy doesn't
问题是我需要的返回类型是CopyableFoo.copy()to CopyableFoo,而不是GenericCopyable。
那可能吗?
编辑:以上是说明问题的示例代码。在这个例子中,以某种方式修改some_code或当然是可能的;CopyableFoo在我的“真实”程序中,这会困难得多。
摇曳的蔷薇
不负相思意
鸿蒙传说
相关分类