假设我有这些课程:
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()
的返回类型必须是CopyableFoo
,而不是GenericCopyable
可能吗
编辑:以上是示例代码来说明问题。在本例中,以某种方式修改some_code
或CopyableFoo
当然是可能的;在我的“真实”计划中,这将是非常困难的
一种可能的解决方案是重写子类中的方法,然后使用指定自己实例的返回类型的子类方法调用超类方法
另一种可能的解决方案是使用键入模块导入Union。这指定父类中的函数能够返回多种类型
你可以这样做
这看起来有点笨拙,因为通常我们不需要注释
self
,因为它隐式地是它绑定到的类的一种类型。 然而,mypy似乎对此没有意见相关问题 更多 >
编程相关推荐