如何解决如何键入提示返回当前类实例的函数?
假设我有这些课程:
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
;在我的“真实”程序中,要困难得多。
解决方法
您可以这样做。
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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。