mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(events): stronger generic typing for event registration
This commit is contained in:
parent
8498d4344b
commit
9d9801b2c2
@ -1,5 +1,5 @@
|
|||||||
from math import floor
|
from math import floor
|
||||||
from typing import TYPE_CHECKING, Any, Coroutine, Optional, Protocol, TypeAlias, TypeVar
|
from typing import TYPE_CHECKING, Any, Coroutine, Generic, Optional, Protocol, TypeAlias, TypeVar
|
||||||
|
|
||||||
from fastapi_events.handlers.local import local_handler
|
from fastapi_events.handlers.local import local_handler
|
||||||
from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny
|
from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny
|
||||||
@ -50,7 +50,7 @@ class EventBase(BaseModel):
|
|||||||
return event_subclasses
|
return event_subclasses
|
||||||
|
|
||||||
|
|
||||||
TEvent = TypeVar("TEvent", bound=EventBase)
|
TEvent = TypeVar("TEvent", bound=EventBase, contravariant=True)
|
||||||
|
|
||||||
FastAPIEvent: TypeAlias = tuple[str, TEvent]
|
FastAPIEvent: TypeAlias = tuple[str, TEvent]
|
||||||
"""
|
"""
|
||||||
@ -59,11 +59,11 @@ Provide a generic type to `TEvent` to specify the payload type.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class FastAPIEventFunc(Protocol):
|
class FastAPIEventFunc(Protocol, Generic[TEvent]):
|
||||||
def __call__(self, event: FastAPIEvent[Any]) -> Optional[Coroutine[Any, Any, None]]: ...
|
def __call__(self, event: FastAPIEvent[TEvent]) -> Optional[Coroutine[Any, Any, None]]: ...
|
||||||
|
|
||||||
|
|
||||||
def register_events(events: set[type[TEvent]] | type[TEvent], func: FastAPIEventFunc) -> None:
|
def register_events(events: set[type[TEvent]] | type[TEvent], func: FastAPIEventFunc[TEvent]) -> None:
|
||||||
"""Register a function to handle specific events.
|
"""Register a function to handle specific events.
|
||||||
|
|
||||||
:param events: An event or set of events to handle
|
:param events: An event or set of events to handle
|
||||||
|
Loading…
Reference in New Issue
Block a user