Skip to content

Commit 2d6b7d2

Browse files
update generic type params
1 parent 3de5999 commit 2d6b7d2

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

i3ipc/aio/connection.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .. import con
77
import os
88
import json
9-
from typing import Callable, Coroutine, Optional, TypeAlias, TypedDict, TypeVar, Union
9+
from typing import Callable, Coroutine, Optional, TypedDict, TypeVar, Union
1010
import struct
1111
import socket
1212
import logging
@@ -33,24 +33,23 @@ def ensure_future(obj):
3333
return future
3434

3535

36-
_BaseEvent = TypeVar('_BaseEvent', bound=IpcBaseEvent, contravariant=True)
36+
_EV = TypeVar('_EV', bound=IpcBaseEvent, contravariant=True)
3737

3838

39-
Handler: TypeAlias = Union[
40-
Callable[['Connection', _BaseEvent], None], Callable[['Connection', _BaseEvent], Coroutine]
41-
]
39+
type Handler[T] = Callable[['Connection', T], None] | Callable[['Connection', T], Coroutine[None, None, None]]
40+
4241

4342

4443
class _Subscription(TypedDict):
4544
event: str
4645
detail: Optional[str]
47-
handler: Handler
46+
handler: Handler[IpcBaseEvent]
4847

4948

5049
class _AIOPubSub(PubSub):
5150
_subscriptions: list[_Subscription] # type: ignore[assignment]
5251

53-
def queue_handler(self, handler: Handler, data: Optional[IpcBaseEvent] = None):
52+
def queue_handler(self, handler: Handler[_EV], data: Optional[IpcBaseEvent] = None):
5453
conn: Connection = self.conn # type: ignore[assignment]
5554

5655
async def handler_coroutine():
@@ -530,8 +529,10 @@ async def subscribe(self, events: Union[list[Event], list[str]], force: bool = F
530529

531530
await self._loop.sock_sendall(self._sub_socket, _pack(MessageType.SUBSCRIBE, payload))
532531

533-
def on(self, event: Union[Event, str], handler: Optional[Handler] = None):
534-
def on_wrapped(handler):
532+
def on(
533+
self, event: Event | str, handler: Handler[_EV] | None = None
534+
) -> Handler[_EV] | Callable[[Handler[_EV]], Handler[_EV]]:
535+
def on_wrapped(handler: Handler[_EV]) -> Handler[_EV]:
535536
self._on(event, handler)
536537
return handler
537538

@@ -540,7 +541,7 @@ def on_wrapped(handler):
540541
else:
541542
return on_wrapped
542543

543-
def _on(self, event: Union[Event, str], handler: Handler):
544+
def _on(self, event: Event | str, handler: Handler[_EV]):
544545
"""Subscribe to the event and call the handler when it is emitted by
545546
the i3 ipc.
546547
@@ -565,7 +566,7 @@ def _on(self, event: Union[Event, str], handler: Handler):
565566
self._pubsub.subscribe(event, handler) # type: ignore[arg-type]
566567
ensure_future(self.subscribe([base_event]))
567568

568-
def off(self, handler: Handler):
569+
def off(self, handler: Handler[_EV]):
569570
"""Unsubscribe the handler from being called on ipc events.
570571
571572
:param handler: The handler that was previously attached with

0 commit comments

Comments
 (0)