Skip to content

Commit a455d96

Browse files
use typevar to control callback argument variance
1 parent 8946ed3 commit a455d96

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

i3ipc/_private/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .pubsub import PubSub
1+
from .pubsub import PubSub, Handler
22
from .types import MessageType, ReplyType, EventType
33
from .sync import Synchronizer

i3ipc/_private/pubsub.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from typing import Callable, Optional, TypeAlias, TypedDict, TYPE_CHECKING
1+
from typing import Callable, Optional, TypeAlias, TypedDict, TypeVar, TYPE_CHECKING
22

33
from i3ipc.events import IpcBaseEvent
44

55
if TYPE_CHECKING:
66
from i3ipc.connection import Connection
77

8-
Handler: TypeAlias = Callable[['Connection', IpcBaseEvent], None]
8+
_BaseEvent = TypeVar('_BaseEvent', bound=IpcBaseEvent, contravariant=True)
9+
10+
Handler: TypeAlias = Callable[['Connection', _BaseEvent], None]
911

1012

1113
class Subscription(TypedDict):

i3ipc/aio/connection.py

Lines changed: 6 additions & 3 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, Union
9+
from typing import Callable, Coroutine, Optional, TypeAlias, TypedDict, TypeVar, Union
1010
import struct
1111
import socket
1212
import logging
@@ -33,8 +33,11 @@ def ensure_future(obj):
3333
return future
3434

3535

36+
_BaseEvent = TypeVar('_BaseEvent', bound=IpcBaseEvent, contravariant=True)
37+
38+
3639
Handler: TypeAlias = Union[
37-
Callable[['Connection', IpcBaseEvent], None], Callable[['Connection', IpcBaseEvent], Coroutine]
40+
Callable[['Connection', _BaseEvent], None], Callable[['Connection', _BaseEvent], Coroutine]
3841
]
3942

4043

@@ -562,7 +565,7 @@ def _on(self, event: Union[Event, str], handler: Handler):
562565
self._pubsub.subscribe(event, handler) # type: ignore[arg-type]
563566
ensure_future(self.subscribe([base_event]))
564567

565-
def off(self, handler: Callable[['Connection', IpcBaseEvent], None]):
568+
def off(self, handler: Handler):
566569
"""Unsubscribe the handler from being called on ipc events.
567570
568571
:param handler: The handler that was previously attached with

i3ipc/connection.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
VersionReply, WorkspaceReply, SeatReply, InputReply)
66
from .events import (IpcBaseEvent, BarconfigUpdateEvent, BindingEvent, OutputEvent, ShutdownEvent,
77
WindowEvent, TickEvent, ModeEvent, WorkspaceEvent, InputEvent, Event)
8-
from ._private import PubSub, MessageType, EventType, Synchronizer
8+
from ._private import PubSub, Handler, MessageType, EventType, Synchronizer
99

10-
from typing import List, Optional, Union, Callable
10+
from typing import List, Optional, Union
1111
import struct
1212
import json
1313
import socket
@@ -380,7 +380,7 @@ def _subscribe(self, events):
380380
self.subscriptions |= events
381381
return result
382382

383-
def off(self, handler: Callable[['Connection', IpcBaseEvent], None]):
383+
def off(self, handler: Handler):
384384
"""Unsubscribe the handler from being called on ipc events.
385385
386386
:param handler: The handler that was previously attached with
@@ -389,9 +389,7 @@ def off(self, handler: Callable[['Connection', IpcBaseEvent], None]):
389389
"""
390390
self._pubsub.unsubscribe(handler)
391391

392-
def on(self,
393-
event: Union[Event, str],
394-
handler: Optional[Callable[['Connection', IpcBaseEvent], None]] = None):
392+
def on(self, event: Union[Event, str], handler: Optional[Handler] = None):
395393
def on_wrapped(handler):
396394
self._on(event, handler)
397395
return handler
@@ -401,7 +399,7 @@ def on_wrapped(handler):
401399
else:
402400
return on_wrapped
403401

404-
def _on(self, event: Union[Event, str], handler: Callable[['Connection', IpcBaseEvent], None]):
402+
def _on(self, event: Union[Event, str], handler: Handler):
405403
"""Subscribe to the event and call the handler when it is emitted by
406404
the i3 ipc.
407405

0 commit comments

Comments
 (0)