From 92a795343f9a04177d86d60a637b85aaea9082d6 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 13:50:51 -0400 Subject: [PATCH 1/6] Adding type hints to methods returning self and children objects --- libtmux/server.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libtmux/server.py b/libtmux/server.py index 5022fe33d..ce46366c9 100644 --- a/libtmux/server.py +++ b/libtmux/server.py @@ -6,6 +6,7 @@ """ import logging import os +import typing as t from . import exc, formats from .common import ( @@ -17,6 +18,10 @@ ) from .session import Session +SessionDict = t.Dict[str, t.Any] +WindowDict = t.Dict[str, t.Any] +PaneDict = t.Dict[str, t.Any] + logger = logging.getLogger(__name__) @@ -125,7 +130,7 @@ def cmd(self, *args, **kwargs): return tmux_cmd(*args, **kwargs) - def _list_sessions(self): + def _list_sessions(self) -> t.List[SessionDict]: """ Return list of sessions in :py:obj:`dict` form. @@ -165,12 +170,12 @@ def _list_sessions(self): return sessions @property - def _sessions(self): + def _sessions(self) -> t.List[SessionDict]: """Property / alias to return :meth:`~._list_sessions`.""" return self._list_sessions() - def list_sessions(self): + def list_sessions(self) -> t.List[Session]: """ Return list of :class:`Session` from the ``tmux(1)`` session. @@ -181,14 +186,14 @@ def list_sessions(self): return [Session(server=self, **s) for s in self._sessions] @property - def sessions(self): + def sessions(self) -> t.List[Session]: """Property / alias to return :meth:`~.list_sessions`.""" return self.list_sessions() #: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject` children = sessions - def _list_windows(self): + def _list_windows(self) -> t.List[WindowDict]: """ Return list of windows in :py:obj:`dict` form. @@ -239,7 +244,7 @@ def _list_windows(self): return self._windows - def _update_windows(self): + def _update_windows(self) -> "Server": """ Update internal window data and return ``self`` for chainability. @@ -250,7 +255,7 @@ def _update_windows(self): self._list_windows() return self - def _list_panes(self): + def _list_panes(self) -> t.List[PaneDict]: """ Return list of panes in :py:obj:`dict` form. @@ -310,7 +315,7 @@ def _list_panes(self): return self._panes - def _update_panes(self): + def _update_panes(self) -> "Server": """ Update internal pane data and return ``self`` for chainability. @@ -322,7 +327,7 @@ def _update_panes(self): return self @property - def attached_sessions(self): + def attached_sessions(self) -> t.Optional[t.List[Session]]: """ Return active :class:`Session` objects. @@ -345,7 +350,7 @@ def attached_sessions(self): return [Session(server=self, **s) for s in attached_sessions] or None - def has_session(self, target_session, exact=True): + def has_session(self, target_session: str, exact: bool = True) -> bool: """ Return True if session exists. ``$ tmux has-session``. @@ -382,7 +387,7 @@ def kill_server(self): """``$ tmux kill-server``.""" self.cmd("kill-server") - def kill_session(self, target_session=None): + def kill_session(self, target_session=None) -> "Server": """ Kill the tmux session with ``$ tmux kill-session``, return ``self``. @@ -462,7 +467,7 @@ def new_session( window_command=None, *args, **kwargs, - ): + ) -> Session: """ Return :class:`Session` from ``$ tmux new-session``. From 92f430f69d78b315301016aff0c3344dead4067c Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 13:59:38 -0400 Subject: [PATCH 2/6] Move SessionDict, WindowDict, and PaneDict to common to use across children objects --- libtmux/common.py | 4 ++++ libtmux/server.py | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index 2964bb540..b71ca4a0a 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -25,6 +25,10 @@ #: Most recent version of tmux supported TMUX_MAX_VERSION = "2.4" +SessionDict = t.Dict[str, t.Any] +WindowDict = t.Dict[str, t.Any] +PaneDict = t.Dict[str, t.Any] + class EnvironmentMixin: diff --git a/libtmux/server.py b/libtmux/server.py index ce46366c9..11f1c1c90 100644 --- a/libtmux/server.py +++ b/libtmux/server.py @@ -11,17 +11,16 @@ from . import exc, formats from .common import ( EnvironmentMixin, + PaneDict, + SessionDict, TmuxRelationalObject, + WindowDict, has_gte_version, session_check_name, tmux_cmd, ) from .session import Session -SessionDict = t.Dict[str, t.Any] -WindowDict = t.Dict[str, t.Any] -PaneDict = t.Dict[str, t.Any] - logger = logging.getLogger(__name__) From d80eb49e94331a480b9262c85027a0939f75bedd Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 14:00:47 -0400 Subject: [PATCH 3/6] Adding type hints to methods returning self and child objects --- libtmux/session.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libtmux/session.py b/libtmux/session.py index 0b3c09b3e..4dd982a02 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -6,12 +6,14 @@ """ import logging import os +import typing as t from . import exc, formats from .common import ( EnvironmentMixin, TmuxMappingObject, TmuxRelationalObject, + WindowDict, handle_option_error, has_version, session_check_name, @@ -129,7 +131,7 @@ def switch_client(self): if proc.stderr: raise exc.LibTmuxException(proc.stderr) - def rename_session(self, new_name): + def rename_session(self, new_name: str) -> "Session": """ Rename session and return new :class:`Session` object. @@ -171,7 +173,7 @@ def new_window( attach=True, window_index="", window_shell=None, - ): + ) -> Window: """ Return :class:`Window` from ``$ tmux new-window``. @@ -273,20 +275,18 @@ def kill_window(self, target_window=None): self.server._update_windows() - def _list_windows(self): + def _list_windows(self) -> t.List[WindowDict]: windows = self.server._update_windows()._windows - windows = [w for w in windows if w["session_id"] == self.id] - - return windows + return [w for w in windows if w["session_id"] == self.id] @property - def _windows(self): + def _windows(self) -> t.List[WindowDict]: """Property / alias to return :meth:`Session._list_windows`.""" return self._list_windows() - def list_windows(self): + def list_windows(self) -> t.List[Window]: """Return a list of :class:`Window` from the ``tmux(1)`` session. Returns @@ -298,7 +298,7 @@ def list_windows(self): return [Window(session=self, **window) for window in windows] @property - def windows(self): + def windows(self) -> t.List[Window]: """Property / alias to return :meth:`Session.list_windows`.""" return self.list_windows() @@ -306,7 +306,7 @@ def windows(self): children = windows @property - def attached_window(self): + def attached_window(self) -> Window: """ Return active :class:`Window` object. @@ -333,7 +333,7 @@ def attached_window(self): if len(self._windows) == int(0): raise exc.LibTmuxException("No Windows") - def select_window(self, target_window): + def select_window(self, target_window: str) -> Window: """ Return :class:`Window` selected via ``$ tmux select-window``. From b81392350acc8827ba9d5f74b4b94957fa8620cf Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 14:07:34 -0400 Subject: [PATCH 4/6] Adding type hints to methods returning self and child objects --- libtmux/window.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/libtmux/window.py b/libtmux/window.py index dbd3052df..4cd398929 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -7,9 +7,15 @@ import logging import os import shlex +import typing as t from . import exc, formats -from .common import TmuxMappingObject, TmuxRelationalObject, handle_option_error +from .common import ( + PaneDict, + TmuxMappingObject, + TmuxRelationalObject, + handle_option_error, +) from .pane import Pane logger = logging.getLogger(__name__) @@ -270,7 +276,7 @@ def show_window_option(self, option, g=False): return option[1] - def rename_window(self, new_name): + def rename_window(self, new_name: str) -> "Window": """ Return :class:`Window` object ``$ tmux rename-window ``. @@ -335,7 +341,7 @@ def move_window(self, destination="", session=None): self.server._update_windows() - def select_window(self): + def select_window(self) -> "Window": """ Select window. Return ``self``. @@ -349,7 +355,7 @@ def select_window(self): """ return self.session.select_window(self.index) - def select_pane(self, target_pane): + def select_pane(self, target_pane: str) -> Pane: """ Return selected :class:`Pane` through ``$ tmux select-pane``. @@ -373,7 +379,7 @@ def select_pane(self, target_pane): return self.attached_pane - def last_pane(self): + def last_pane(self) -> Pane: """Return last pane.""" return self.select_pane("-l") @@ -385,7 +391,7 @@ def split_window( vertical=True, shell=None, percent=None, - ): + ) -> Pane: """ Split window and return the created :class:`Pane`. @@ -486,7 +492,7 @@ def split_window( return Pane(window=self, **pane) @property - def attached_pane(self): + def attached_pane(self) -> Pane: """ Return the attached :class:`Pane`. @@ -504,7 +510,7 @@ def attached_pane(self): return [] - def _list_panes(self): + def _list_panes(self) -> t.List[PaneDict]: panes = self.server._update_panes()._panes panes = [p for p in panes if p["session_id"] == self.get("session_id")] @@ -512,12 +518,12 @@ def _list_panes(self): return panes @property - def _panes(self): + def _panes(self) -> t.List[PaneDict]: """Property / alias to return :meth:`~._list_panes`.""" return self._list_panes() - def list_panes(self): + def list_panes(self) -> t.List[Pane]: """ Return list of :class:`Pane` for the window. @@ -529,7 +535,7 @@ def list_panes(self): return [Pane(window=self, **pane) for pane in self._panes] @property - def panes(self): + def panes(self) -> t.List[Pane]: """Property / alias to return :meth:`~.list_panes`.""" return self.list_panes() From b4cc8d2840ab30642d86bfe3444c01e9f2374792 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 14:10:25 -0400 Subject: [PATCH 5/6] Refactor attached_pane and update return type to Optional --- libtmux/window.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/libtmux/window.py b/libtmux/window.py index 4cd398929..c8c3ce9b7 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -355,7 +355,7 @@ def select_window(self) -> "Window": """ return self.session.select_window(self.index) - def select_pane(self, target_pane: str) -> Pane: + def select_pane(self, target_pane: str) -> t.Optional[Pane]: """ Return selected :class:`Pane` through ``$ tmux select-pane``. @@ -379,7 +379,7 @@ def select_pane(self, target_pane: str) -> Pane: return self.attached_pane - def last_pane(self) -> Pane: + def last_pane(self) -> t.Optional[Pane]: """Return last pane.""" return self.select_pane("-l") @@ -492,7 +492,7 @@ def split_window( return Pane(window=self, **pane) @property - def attached_pane(self) -> Pane: + def attached_pane(self) -> t.Optional[Pane]: """ Return the attached :class:`Pane`. @@ -505,10 +505,6 @@ def attached_pane(self) -> Pane: # for now pane_active is a unicode if pane.get("pane_active") == "1": return Pane(window=self, **pane) - else: - continue - - return [] def _list_panes(self) -> t.List[PaneDict]: panes = self.server._update_panes()._panes From fcfeb39d38412fe9a63a3d3008a01dc7b0d00a63 Mon Sep 17 00:00:00 2001 From: otherJL0 Date: Fri, 18 Mar 2022 14:12:41 -0400 Subject: [PATCH 6/6] Adding type hints to methods returning self --- libtmux/pane.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libtmux/pane.py b/libtmux/pane.py index 510da1d32..8ccf354fd 100644 --- a/libtmux/pane.py +++ b/libtmux/pane.py @@ -154,7 +154,7 @@ def reset(self): def split_window( self, attach=False, vertical=True, start_directory=None, percent=None - ): + ) -> "Pane": """ Split window at pane and return newly created :class:`Pane`. @@ -261,7 +261,7 @@ def capture_pane(self): """ return self.cmd("capture-pane", "-p").stdout - def select_pane(self): + def select_pane(self) -> "Pane": """ Select pane. Return ``self``. @@ -275,7 +275,7 @@ def select_pane(self): """ return self.window.select_pane(self.get("pane_id")) - def __repr__(self): + def __repr__(self) -> str: return "{}({} {})".format( self.__class__.__name__, self.get("pane_id"), self.window )