如何完善python类型的提示,以提示在继承类时返回类实例的方法?
我有一个父母,其方法可以返回该类的实例。 该课程有一个(或几个)儿童课。
如何提供类型提示以表明任何子类中的方法调用将返回该孩子类的实例,而不是父母类的实例?
例如,使用以下代码:
from random import randint
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def get_random_person(cls) -> "Person":
return cls("Random Guy", randint(18, 65))
class Player(Person):
pass
def welcome_player(player: Player):
print(f"Welcome {player.name}")
player = Player.get_random_person()
welcome_player(player)
linter mypy
会引起错误:
error: Argument 1 to "welcome_player" has incompatible type "Person"; expected "Player"
Found 1 error in 1 file (checked 1 source file)
当然,您可以执行以下操作,但是如果父母类多次继承,那就很重:
from random import randint
from typing import cast
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def get_random_person(cls) -> "Person":
return cls("Random Guy", randint(18, 65))
class Player(Person):
@classmethod
def get_random_person(cls) -> "Player":
return cast(Player, super().get_random_person())
def welcome_player(player: Player):
print(f"Welcome {player.name}")
player = Player.get_random_person()
welcome_player(player)
任何建议:以明智的方式实现这一目标?
I have a parent class with a method that returns an instance of that class.
This class has one (or several) children classes.
How to provide type hints to indicate that the method call on any child class will return an instance of that very child class and not the parent class?
For example, with the following code:
from random import randint
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def get_random_person(cls) -> "Person":
return cls("Random Guy", randint(18, 65))
class Player(Person):
pass
def welcome_player(player: Player):
print(f"Welcome {player.name}")
player = Player.get_random_person()
welcome_player(player)
The linter mypy
would raise an error:
error: Argument 1 to "welcome_player" has incompatible type "Person"; expected "Player"
Found 1 error in 1 file (checked 1 source file)
Of course, you could do something like the following, but that is very heavy if the parent class is inherited multiple times:
from random import randint
from typing import cast
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def get_random_person(cls) -> "Person":
return cls("Random Guy", randint(18, 65))
class Player(Person):
@classmethod
def get_random_person(cls) -> "Player":
return cast(Player, super().get_random_person())
def welcome_player(player: Player):
print(f"Welcome {player.name}")
player = Player.get_random_person()
welcome_player(player)
Any suggestion on a smart way to achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
typing.typing.typevar
typing 使用参数BOND
模拟:Python 3.11将添加
self code> self code> 但尚未正式发布。
键入 - 延迟
在较旧版本中可以访问,尽管mypy
会抱怨错误:变量“ typing_extensions.self”无效作为类型)。
You can use
typing.TypeVar
with parameterbound
to simulate:Python 3.11 will add the
Self
type, but it has not been officially released yet. This is accessible in older versions with thetyping-extensions
, althoughmypy
will complain thaterror: Variable "typing_extensions.Self" is not valid as a type
).