diff --git a/libtmux/_compat.py b/libtmux/_compat.py index a1c5bfe99..d16df9fb7 100644 --- a/libtmux/_compat.py +++ b/libtmux/_compat.py @@ -1,7 +1,6 @@ # flake8: NOQA import sys import typing as t -from collections.abc import MutableMapping console_encoding = sys.__stdout__.encoding diff --git a/libtmux/common.py b/libtmux/common.py index b71ca4a0a..05b85229f 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -11,10 +11,11 @@ import subprocess import sys import typing as t +from collections.abc import MutableMapping from distutils.version import LooseVersion from . import exc -from ._compat import MutableMapping, console_to_str, str_from_console +from ._compat import console_to_str, str_from_console logger = logging.getLogger(__name__) @@ -62,7 +63,7 @@ def set_environment(self, name, value): proc = self.cmd(*args) if proc.stderr: - if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): + if isinstance(proc.stderr, list) and len(proc.stderr) == 1: proc.stderr = proc.stderr[0] raise ValueError("tmux set-environment stderr: %s" % proc.stderr) @@ -83,7 +84,7 @@ def unset_environment(self, name): proc = self.cmd(*args) if proc.stderr: - if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): + if isinstance(proc.stderr, list) and len(proc.stderr) == 1: proc.stderr = proc.stderr[0] raise ValueError("tmux set-environment stderr: %s" % proc.stderr) @@ -103,7 +104,7 @@ def remove_environment(self, name): proc = self.cmd(*args) if proc.stderr: - if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): + if isinstance(proc.stderr, list) and len(proc.stderr) == 1: proc.stderr = proc.stderr[0] raise ValueError("tmux set-environment stderr: %s" % proc.stderr) @@ -336,19 +337,20 @@ def where(self, attrs, first=False): """ # from https://github.com/serkanyersen/underscore.py - def by(val, *args): - for key, value in attrs.items(): + def by(val) -> bool: + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True + # TODO add type hint + target_children = list(filter(by, self.children)) if first: - return list(filter(by, self.children))[0] - else: - return list(filter(by, self.children)) + return target_children[0] + return target_children def get_by_id(self, id): """ diff --git a/libtmux/pane.py b/libtmux/pane.py index 8ccf354fd..e35a70f65 100644 --- a/libtmux/pane.py +++ b/libtmux/pane.py @@ -58,21 +58,23 @@ def __init__(self, window=None, **kwargs): self.server._update_panes() @property - def _info(self, *args): + def _info(self): attrs = {"pane_id": self._pane_id} # from https://github.com/serkanyersen/underscore.py - def by(val, *args): - for key, value in attrs.items(): + def by(val) -> bool: + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True - return list(filter(by, self.server._panes))[0] + # TODO add type hint + target_panes = list(filter(by, self.server._panes)) + return target_panes[0] def cmd(self, cmd, *args, **kwargs): """Return :meth:`Server.cmd` defaulting to ``target_pane`` as target. diff --git a/libtmux/session.py b/libtmux/session.py index 4dd982a02..e3a390ec0 100644 --- a/libtmux/session.py +++ b/libtmux/session.py @@ -65,17 +65,19 @@ def _info(self): attrs = {"session_id": str(self._session_id)} - def by(val): - for key, value in attrs.items(): + def by(val) -> bool: + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True + # TODO add type hint + target_sessions = list(filter(by, self.server._sessions)) try: - return list(filter(by, self.server._sessions))[0] + return target_sessions[0] except IndexError as e: logger.error(e) @@ -316,21 +318,18 @@ def attached_window(self) -> Window: """ active_windows = [] for window in self._windows: - if "window_active" in window: - # for now window_active is a unicode - if window.get("window_active") == "1": - active_windows.append(Window(session=self, **window)) - else: - continue - - if len(active_windows) == int(1): + # for now window_active is a unicode + if "window_active" in window and window.get("window_active") == "1": + active_windows.append(Window(session=self, **window)) + + if len(active_windows) == 1: return active_windows[0] else: raise exc.LibTmuxException( "multiple active windows found. %s" % active_windows ) - if len(self._windows) == int(0): + if len(self._windows) == 0: raise exc.LibTmuxException("No Windows") def select_window(self, target_window: str) -> Window: diff --git a/libtmux/window.py b/libtmux/window.py index c8c3ce9b7..386310005 100644 --- a/libtmux/window.py +++ b/libtmux/window.py @@ -74,22 +74,23 @@ def _info(self, *args): attrs = {"window_id": self._window_id} # from https://github.com/serkanyersen/underscore.py - def by(val, *args): - for key, value in attrs.items(): + def by(val) -> bool: + for key in attrs.keys(): try: if attrs[key] != val[key]: return False except KeyError: return False - return True + return True - ret = list(filter(by, self.server._windows)) + # TODO add type hint + target_windows = list(filter(by, self.server._windows)) # If a window_shell option was configured which results in # a short-lived process, the window id is @0. Use that instead of # self._window_id - if len(ret) == 0 and self.server._windows[0]["window_id"] == "@0": - ret = self.server._windows - return ret[0] + if len(target_windows) == 0 and self.server._windows[0]["window_id"] == "@0": + target_windows = self.server._windows + return target_windows[0] def cmd(self, cmd, *args, **kwargs): """Return :meth:`Server.cmd` defaulting ``target_window`` as target. @@ -501,10 +502,9 @@ def attached_pane(self) -> t.Optional[Pane]: :class:`Pane` """ for pane in self._panes: - if "pane_active" in pane: - # for now pane_active is a unicode - if pane.get("pane_active") == "1": - return Pane(window=self, **pane) + # for now pane_active is a unicode + if "pane_active" in pane and pane.get("pane_active") == "1": + return Pane(window=self, **pane) def _list_panes(self) -> t.List[PaneDict]: panes = self.server._update_panes()._panes