Inherit multiple times from same base object with different Generics

59 views Asked by At

I have an Object class in my library implementing event logic for a given set of events with type hints:

class Event(enum.Enum):
    pass

EventT = TypeVar("EventT", bound=Event)

class Object(Generic[EventT]):
    def bind(self, event: EventT, callback): ...
    def fire(self, event: EventT): ...

I then have a Container class who inherits from Object:

class ContainerEvent(Event):
    ITEM_ADDED = auto()
    ITEM_REMOVED = auto()

ContainedT = TypeVar("ContainedT")

class Container(Generic[ContainedT], Object[ContainerEvent]):
    def add(self, item_id, item: ContainedT): ...
    def get(self, item_id) -> ContainedT: ...
    def remove(self, item_id): ...

Let's say I want to add an Application class. It's a Window Container, but also an Object with its own Events.

class ApplicationEvent(Event):
    START = auto()

class Application(Container[Window], Object[ApplicationEvent]):
    def start():
        self.fire(ApplicationEvent.START)

However, this fails since both Application and Container inherit from Object:

Base classes of Application are mutually incompatible
  Base class "Object[ApplicationEvent]" is incompatible with type "Object[ContainerEvent]"

How can I make Application be both a Container and an Object? Without type hints, I can inherit one time from Object, so how can I do it with type hinting?

0

There are 0 answers