如何键入提示 returns 当前 class 实例的函数?
How do I type hint a function that returns an instance of the current class?
假设我有这些 类:
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()
的 return 类型是 CopyableFoo
,而不是 GenericCopyable
。
这可能吗?
已编辑:以上是说明问题的示例代码。在这个例子中,以某种方式修改 some_code
或 CopyableFoo
当然是可能的;在我的“真实”程序中,这要困难得多。
一种可能的解决方案是重写 children class 中的方法,然后使用 children class 方法调用您的 superclass 方法指定他们自己实例的 return 类型。
class GenericCopyable:
def copy(self) -> GenericCopyable:
... # whatever is required to copy this
class CopyableFoo(GenericCopyable):
def copy(self)->CopyableFoo:
return super().copy()
另一种可能的解决方案是使用 typing 模块导入 Union。这指定你的parentclass中的函数能够return多种类型
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()
你可以做到这一点。
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
,因为它隐含地是它绑定到的 class 的一种类型。
不过mypy好像还可以吧
假设我有这些 类:
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()
的 return 类型是 CopyableFoo
,而不是 GenericCopyable
。
这可能吗?
已编辑:以上是说明问题的示例代码。在这个例子中,以某种方式修改 some_code
或 CopyableFoo
当然是可能的;在我的“真实”程序中,这要困难得多。
一种可能的解决方案是重写 children class 中的方法,然后使用 children class 方法调用您的 superclass 方法指定他们自己实例的 return 类型。
class GenericCopyable:
def copy(self) -> GenericCopyable:
... # whatever is required to copy this
class CopyableFoo(GenericCopyable):
def copy(self)->CopyableFoo:
return super().copy()
另一种可能的解决方案是使用 typing 模块导入 Union。这指定你的parentclass中的函数能够return多种类型
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()
你可以做到这一点。
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
,因为它隐含地是它绑定到的 class 的一种类型。
不过mypy好像还可以吧