Skip to content

Adding type hints to class methods returning self and child #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
6 changes: 3 additions & 3 deletions libtmux/pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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``.

Expand All @@ -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
)
28 changes: 16 additions & 12 deletions libtmux/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
"""
import logging
import os
import typing as t

from . import exc, formats
from .common import (
EnvironmentMixin,
PaneDict,
SessionDict,
TmuxRelationalObject,
WindowDict,
has_gte_version,
session_check_name,
tmux_cmd,
Expand Down Expand Up @@ -125,7 +129,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.

Expand Down Expand Up @@ -165,12 +169,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.

Expand All @@ -181,14 +185,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.

Expand Down Expand Up @@ -239,7 +243,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.

Expand All @@ -250,7 +254,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.

Expand Down Expand Up @@ -310,7 +314,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.

Expand All @@ -322,7 +326,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.

Expand All @@ -345,7 +349,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``.

Expand Down Expand Up @@ -382,7 +386,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``.

Expand Down Expand Up @@ -462,7 +466,7 @@ def new_session(
window_command=None,
*args,
**kwargs,
):
) -> Session:
"""
Return :class:`Session` from ``$ tmux new-session``.

Expand Down
22 changes: 11 additions & 11 deletions libtmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -171,7 +173,7 @@ def new_window(
attach=True,
window_index="",
window_shell=None,
):
) -> Window:
"""
Return :class:`Window` from ``$ tmux new-window``.

Expand Down Expand Up @@ -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
Expand All @@ -298,15 +298,15 @@ 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()

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

@property
def attached_window(self):
def attached_window(self) -> Window:
"""
Return active :class:`Window` object.

Expand All @@ -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``.

Expand Down
32 changes: 17 additions & 15 deletions libtmux/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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 <new_name>``.

Expand Down Expand Up @@ -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``.

Expand All @@ -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) -> t.Optional[Pane]:
"""
Return selected :class:`Pane` through ``$ tmux select-pane``.

Expand All @@ -373,7 +379,7 @@ def select_pane(self, target_pane):

return self.attached_pane

def last_pane(self):
def last_pane(self) -> t.Optional[Pane]:
"""Return last pane."""
return self.select_pane("-l")

Expand All @@ -385,7 +391,7 @@ def split_window(
vertical=True,
shell=None,
percent=None,
):
) -> Pane:
"""
Split window and return the created :class:`Pane`.

Expand Down Expand Up @@ -486,7 +492,7 @@ def split_window(
return Pane(window=self, **pane)

@property
def attached_pane(self):
def attached_pane(self) -> t.Optional[Pane]:
"""
Return the attached :class:`Pane`.

Expand All @@ -499,25 +505,21 @@ def attached_pane(self):
# 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):
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")]
panes = [p for p in panes if p["window_id"] == self.id]
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.

Expand All @@ -529,7 +531,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()

Expand Down