如何键入提示返回当前类实例的函数?

问题描述

假设我有这些课程:

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_codeCopyableFoo;在我的“真实”程序中,要困难得多。

解决方法

您可以这样做。

from typing import TypeVar
# We define T as a TypeVar bound to the base class GenericCopyable
T = TypeVar('T',bound='GenericCopyable')

class GenericCopyable:
    # we return the type T of the type of self
    # Basically returning an instance of the calling
    # type's class
    def copy(self: T) -> T:
        return type(self)()

class CopyableFoo(GenericCopyable):
    pass

foo = CopyableFoo()

bar = foo.copy()
print(bar)

这看起来有点笨拙,因为通常我们不需要注释self,因为它隐式是它绑定到的类的一种。 但是,mypy似乎还可以。

,

一种可能的解决方案是重写子类中的方法,然后使用子类方法指定自己实例的返回类型来调用您的超类方法。

class GenericCopyable:
    def copy(self) -> GenericCopyable:
        ... # whatever is required to copy this

class CopyableFoo(GenericCopyable):
   def copy(self)->CopyableFoo:
       return super().copy()

另一种可能的解决方案是使用键入模块导入Union。这指定您父类中的函数能够返回多种类型


from typing import Union

class GenericCopyable:
    def copy(self) -> Union[GenericCopyable,CopyableFoo]:
        ... # whatever is required to copy this

class CopyableFoo(GenericCopyable):
    #Call parent class method directly
    GenericCopyable.copy()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...