Skip to content

Commit 8946ed3

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

File tree

9 files changed

+325
-168
lines changed

9 files changed

+325
-168
lines changed

i3ipc/_private/pubsub.py

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

6-
def subscribe(self, detailed_event, handler):
22+
def subscribe(self, detailed_event: str, handler: Handler):
723
event = detailed_event.replace('-', '_')
824
detail = ''
925

@@ -12,10 +28,10 @@ def subscribe(self, detailed_event, handler):
1228

1329
self._subscriptions.append({'event': event, 'detail': detail, 'handler': handler})
1430

15-
def unsubscribe(self, handler):
31+
def unsubscribe(self, handler: Handler):
1632
self._subscriptions = list(filter(lambda s: s['handler'] != handler, self._subscriptions))
1733

18-
def emit(self, event, data):
34+
def emit(self, event: str, data: Optional[IpcBaseEvent]):
1935
detail = ''
2036

2137
if data and hasattr(data, 'change'):
@@ -27,4 +43,4 @@ def emit(self, event, data):
2743
if data:
2844
s['handler'](self.conn, data)
2945
else:
30-
s['handler'](self.conn)
46+
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)