-
Hello. I'm developing a game and its very heavy python code will have a mixin design similar to the one below. I hope to mark the However, pyright will consider this kind of annotation illegal and report Please help me, how should I write the type annotation for this mixin class? Or how to avoid pright reporting these errors. class Log(object):
def log_debug(self: "Entity", msg):
pass
class Bag(object):
def add_item(self: "Entity"):
self.log_debug("")
class Entity(Bag, Log):
pass
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The way this is typically done is through the use of a protocol class. from typing import Protocol
class SupportsLogDebug(Protocol):
def log_debug(self, msg: str): ...
class Log(SupportsLogDebug):
def log_debug(self, msg: str):
pass
class Bag:
def add_item(self: SupportsLogDebug):
self.log_debug("")
class Entity(Bag, Log):
pass Or if you don't want to define a separate protocol class, you could do: from typing import Protocol
class Log(Protocol):
def log_debug(self, msg: str):
pass
class Bag:
def add_item(self: Log):
self.log_debug("")
class Entity(Bag, Log):
pass In general, this isn't a pattern I would recommend in object-oriented programming. The way you've defined this class hierarchy, the parent class |
Beta Was this translation helpful? Give feedback.
-
You can have all your mix-in classes subclass from typing import Protocol
class Log(Protocol):
def log_debug(self: "Entity", msg):
pass
class Bag(Protocol):
def add_item(self: "Entity"):
self.log_debug("")
class Entity(Bag, Log):
pass |
Beta Was this translation helpful? Give feedback.
The way this is typically done is through the use of a protocol class.
Or if you don't want to define a separate protocol class, you could do:
In general, this isn't a pattern I would recommend in objec…