Skip to content

help for type hints for mixin class #7923

Answered by erictraut
prim asked this question in Q&A
Discussion options

You must be logged in to vote

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 objec…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by prim
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants