Skip to content

Commit 45889dc

Browse files
authored
Adding type hints to class methods returning self and child (#361)
* Adding `SessionDict`, `WindowDict`, and `PaneDict` type aliases to `common.py` for _list methods * Adding hints for methods returning `self` and collections of children
2 parents c30da96 + fcfeb39 commit 45889dc

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

libtmux/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#: Most recent version of tmux supported
2626
TMUX_MAX_VERSION = "2.4"
2727

28+
SessionDict = t.Dict[str, t.Any]
29+
WindowDict = t.Dict[str, t.Any]
30+
PaneDict = t.Dict[str, t.Any]
31+
2832

2933
class EnvironmentMixin:
3034

libtmux/pane.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def reset(self):
154154

155155
def split_window(
156156
self, attach=False, vertical=True, start_directory=None, percent=None
157-
):
157+
) -> "Pane":
158158
"""
159159
Split window at pane and return newly created :class:`Pane`.
160160
@@ -261,7 +261,7 @@ def capture_pane(self):
261261
"""
262262
return self.cmd("capture-pane", "-p").stdout
263263

264-
def select_pane(self):
264+
def select_pane(self) -> "Pane":
265265
"""
266266
Select pane. Return ``self``.
267267
@@ -275,7 +275,7 @@ def select_pane(self):
275275
"""
276276
return self.window.select_pane(self.get("pane_id"))
277277

278-
def __repr__(self):
278+
def __repr__(self) -> str:
279279
return "{}({} {})".format(
280280
self.__class__.__name__, self.get("pane_id"), self.window
281281
)

libtmux/server.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
"""
77
import logging
88
import os
9+
import typing as t
910

1011
from . import exc, formats
1112
from .common import (
1213
EnvironmentMixin,
14+
PaneDict,
15+
SessionDict,
1316
TmuxRelationalObject,
17+
WindowDict,
1418
has_gte_version,
1519
session_check_name,
1620
tmux_cmd,
@@ -125,7 +129,7 @@ def cmd(self, *args, **kwargs):
125129

126130
return tmux_cmd(*args, **kwargs)
127131

128-
def _list_sessions(self):
132+
def _list_sessions(self) -> t.List[SessionDict]:
129133
"""
130134
Return list of sessions in :py:obj:`dict` form.
131135
@@ -165,12 +169,12 @@ def _list_sessions(self):
165169
return sessions
166170

167171
@property
168-
def _sessions(self):
172+
def _sessions(self) -> t.List[SessionDict]:
169173
"""Property / alias to return :meth:`~._list_sessions`."""
170174

171175
return self._list_sessions()
172176

173-
def list_sessions(self):
177+
def list_sessions(self) -> t.List[Session]:
174178
"""
175179
Return list of :class:`Session` from the ``tmux(1)`` session.
176180
@@ -181,14 +185,14 @@ def list_sessions(self):
181185
return [Session(server=self, **s) for s in self._sessions]
182186

183187
@property
184-
def sessions(self):
188+
def sessions(self) -> t.List[Session]:
185189
"""Property / alias to return :meth:`~.list_sessions`."""
186190
return self.list_sessions()
187191

188192
#: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject`
189193
children = sessions
190194

191-
def _list_windows(self):
195+
def _list_windows(self) -> t.List[WindowDict]:
192196
"""
193197
Return list of windows in :py:obj:`dict` form.
194198
@@ -239,7 +243,7 @@ def _list_windows(self):
239243

240244
return self._windows
241245

242-
def _update_windows(self):
246+
def _update_windows(self) -> "Server":
243247
"""
244248
Update internal window data and return ``self`` for chainability.
245249
@@ -250,7 +254,7 @@ def _update_windows(self):
250254
self._list_windows()
251255
return self
252256

253-
def _list_panes(self):
257+
def _list_panes(self) -> t.List[PaneDict]:
254258
"""
255259
Return list of panes in :py:obj:`dict` form.
256260
@@ -310,7 +314,7 @@ def _list_panes(self):
310314

311315
return self._panes
312316

313-
def _update_panes(self):
317+
def _update_panes(self) -> "Server":
314318
"""
315319
Update internal pane data and return ``self`` for chainability.
316320
@@ -322,7 +326,7 @@ def _update_panes(self):
322326
return self
323327

324328
@property
325-
def attached_sessions(self):
329+
def attached_sessions(self) -> t.Optional[t.List[Session]]:
326330
"""
327331
Return active :class:`Session` objects.
328332
@@ -345,7 +349,7 @@ def attached_sessions(self):
345349

346350
return [Session(server=self, **s) for s in attached_sessions] or None
347351

348-
def has_session(self, target_session, exact=True):
352+
def has_session(self, target_session: str, exact: bool = True) -> bool:
349353
"""
350354
Return True if session exists. ``$ tmux has-session``.
351355
@@ -382,7 +386,7 @@ def kill_server(self):
382386
"""``$ tmux kill-server``."""
383387
self.cmd("kill-server")
384388

385-
def kill_session(self, target_session=None):
389+
def kill_session(self, target_session=None) -> "Server":
386390
"""
387391
Kill the tmux session with ``$ tmux kill-session``, return ``self``.
388392
@@ -462,7 +466,7 @@ def new_session(
462466
window_command=None,
463467
*args,
464468
**kwargs,
465-
):
469+
) -> Session:
466470
"""
467471
Return :class:`Session` from ``$ tmux new-session``.
468472

libtmux/session.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
"""
77
import logging
88
import os
9+
import typing as t
910

1011
from . import exc, formats
1112
from .common import (
1213
EnvironmentMixin,
1314
TmuxMappingObject,
1415
TmuxRelationalObject,
16+
WindowDict,
1517
handle_option_error,
1618
has_version,
1719
session_check_name,
@@ -129,7 +131,7 @@ def switch_client(self):
129131
if proc.stderr:
130132
raise exc.LibTmuxException(proc.stderr)
131133

132-
def rename_session(self, new_name):
134+
def rename_session(self, new_name: str) -> "Session":
133135
"""
134136
Rename session and return new :class:`Session` object.
135137
@@ -171,7 +173,7 @@ def new_window(
171173
attach=True,
172174
window_index="",
173175
window_shell=None,
174-
):
176+
) -> Window:
175177
"""
176178
Return :class:`Window` from ``$ tmux new-window``.
177179
@@ -273,20 +275,18 @@ def kill_window(self, target_window=None):
273275

274276
self.server._update_windows()
275277

276-
def _list_windows(self):
278+
def _list_windows(self) -> t.List[WindowDict]:
277279
windows = self.server._update_windows()._windows
278280

279-
windows = [w for w in windows if w["session_id"] == self.id]
280-
281-
return windows
281+
return [w for w in windows if w["session_id"] == self.id]
282282

283283
@property
284-
def _windows(self):
284+
def _windows(self) -> t.List[WindowDict]:
285285
"""Property / alias to return :meth:`Session._list_windows`."""
286286

287287
return self._list_windows()
288288

289-
def list_windows(self):
289+
def list_windows(self) -> t.List[Window]:
290290
"""Return a list of :class:`Window` from the ``tmux(1)`` session.
291291
292292
Returns
@@ -298,15 +298,15 @@ def list_windows(self):
298298
return [Window(session=self, **window) for window in windows]
299299

300300
@property
301-
def windows(self):
301+
def windows(self) -> t.List[Window]:
302302
"""Property / alias to return :meth:`Session.list_windows`."""
303303
return self.list_windows()
304304

305305
#: Alias :attr:`windows` for :class:`~libtmux.common.TmuxRelationalObject`
306306
children = windows
307307

308308
@property
309-
def attached_window(self):
309+
def attached_window(self) -> Window:
310310
"""
311311
Return active :class:`Window` object.
312312
@@ -333,7 +333,7 @@ def attached_window(self):
333333
if len(self._windows) == int(0):
334334
raise exc.LibTmuxException("No Windows")
335335

336-
def select_window(self, target_window):
336+
def select_window(self, target_window: str) -> Window:
337337
"""
338338
Return :class:`Window` selected via ``$ tmux select-window``.
339339

libtmux/window.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import logging
88
import os
99
import shlex
10+
import typing as t
1011

1112
from . import exc, formats
12-
from .common import TmuxMappingObject, TmuxRelationalObject, handle_option_error
13+
from .common import (
14+
PaneDict,
15+
TmuxMappingObject,
16+
TmuxRelationalObject,
17+
handle_option_error,
18+
)
1319
from .pane import Pane
1420

1521
logger = logging.getLogger(__name__)
@@ -270,7 +276,7 @@ def show_window_option(self, option, g=False):
270276

271277
return option[1]
272278

273-
def rename_window(self, new_name):
279+
def rename_window(self, new_name: str) -> "Window":
274280
"""
275281
Return :class:`Window` object ``$ tmux rename-window <new_name>``.
276282
@@ -335,7 +341,7 @@ def move_window(self, destination="", session=None):
335341

336342
self.server._update_windows()
337343

338-
def select_window(self):
344+
def select_window(self) -> "Window":
339345
"""
340346
Select window. Return ``self``.
341347
@@ -349,7 +355,7 @@ def select_window(self):
349355
"""
350356
return self.session.select_window(self.index)
351357

352-
def select_pane(self, target_pane):
358+
def select_pane(self, target_pane: str) -> t.Optional[Pane]:
353359
"""
354360
Return selected :class:`Pane` through ``$ tmux select-pane``.
355361
@@ -373,7 +379,7 @@ def select_pane(self, target_pane):
373379

374380
return self.attached_pane
375381

376-
def last_pane(self):
382+
def last_pane(self) -> t.Optional[Pane]:
377383
"""Return last pane."""
378384
return self.select_pane("-l")
379385

@@ -385,7 +391,7 @@ def split_window(
385391
vertical=True,
386392
shell=None,
387393
percent=None,
388-
):
394+
) -> Pane:
389395
"""
390396
Split window and return the created :class:`Pane`.
391397
@@ -486,7 +492,7 @@ def split_window(
486492
return Pane(window=self, **pane)
487493

488494
@property
489-
def attached_pane(self):
495+
def attached_pane(self) -> t.Optional[Pane]:
490496
"""
491497
Return the attached :class:`Pane`.
492498
@@ -499,25 +505,21 @@ def attached_pane(self):
499505
# for now pane_active is a unicode
500506
if pane.get("pane_active") == "1":
501507
return Pane(window=self, **pane)
502-
else:
503-
continue
504508

505-
return []
506-
507-
def _list_panes(self):
509+
def _list_panes(self) -> t.List[PaneDict]:
508510
panes = self.server._update_panes()._panes
509511

510512
panes = [p for p in panes if p["session_id"] == self.get("session_id")]
511513
panes = [p for p in panes if p["window_id"] == self.id]
512514
return panes
513515

514516
@property
515-
def _panes(self):
517+
def _panes(self) -> t.List[PaneDict]:
516518
"""Property / alias to return :meth:`~._list_panes`."""
517519

518520
return self._list_panes()
519521

520-
def list_panes(self):
522+
def list_panes(self) -> t.List[Pane]:
521523
"""
522524
Return list of :class:`Pane` for the window.
523525
@@ -529,7 +531,7 @@ def list_panes(self):
529531
return [Pane(window=self, **pane) for pane in self._panes]
530532

531533
@property
532-
def panes(self):
534+
def panes(self) -> t.List[Pane]:
533535
"""Property / alias to return :meth:`~.list_panes`."""
534536
return self.list_panes()
535537

0 commit comments

Comments
 (0)