Skip to content

Commit 4dce628

Browse files
Add py.typed and annotate most of the classes and methods
1 parent 9de8e88 commit 4dce628

File tree

9 files changed

+319
-167
lines changed

9 files changed

+319
-167
lines changed

i3ipc/_private/pubsub.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
from typing import Callable, Optional, TypeAlias, TypedDict
2+
3+
from i3ipc.connection import Connection
4+
from i3ipc.events import IpcBaseEvent
5+
6+
Handler: TypeAlias = Callable[[Connection, IpcBaseEvent], None]
7+
8+
9+
class Subscription(TypedDict):
10+
event: str
11+
detail: Optional[str]
12+
handler: Handler
13+
14+
115
class PubSub(object):
2-
def __init__(self, conn):
16+
def __init__(self, conn: Connection):
317
self.conn = conn
4-
self._subscriptions = []
18+
self._subscriptions: list[Subscription] = []
519

6-
def subscribe(self, detailed_event, handler):
20+
def subscribe(self, detailed_event: str, handler: Handler):
721
event = detailed_event.replace('-', '_')
822
detail = ''
923

@@ -12,10 +26,10 @@ def subscribe(self, detailed_event, handler):
1226

1327
self._subscriptions.append({'event': event, 'detail': detail, 'handler': handler})
1428

15-
def unsubscribe(self, handler):
29+
def unsubscribe(self, handler: Handler):
1630
self._subscriptions = list(filter(lambda s: s['handler'] != handler, self._subscriptions))
1731

18-
def emit(self, event, data):
32+
def emit(self, event: str, data: Optional[IpcBaseEvent]):
1933
detail = ''
2034

2135
if data and hasattr(data, 'change'):
@@ -27,4 +41,4 @@ def emit(self, event, data):
2741
if data:
2842
s['handler'](self.conn, data)
2943
else:
30-
s['handler'](self.conn)
44+
s['handler'](self.conn) # type: ignore[call-arg]

i3ipc/_private/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ class EventType(Enum):
4343
TICK = (1 << 7)
4444
INPUT = (1 << 21)
4545

46-
def to_string(self):
46+
def to_string(self) -> str:
4747
return str.lower(self.name)
4848

4949
@staticmethod
50-
def from_string(val):
50+
def from_string(val) -> 'EventType':
5151
match = [e for e in EventType if e.to_string() == val]
5252

5353
if not match:
5454
raise ValueError('event not implemented: ' + val)
5555

5656
return match[0]
5757

58-
def to_list(self):
58+
def to_list(self) -> list[str]:
5959
events_list = []
6060
if self.value & EventType.WORKSPACE.value:
6161
events_list.append(EventType.WORKSPACE.to_string())

0 commit comments

Comments
 (0)