From e905021a76937ba60a6d15b675cb585bf53d7672 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Thu, 4 Aug 2022 21:56:07 -0500 Subject: [PATCH 1/3] refactor(common): Generic improvements for TmuxRelationalObject --- libtmux/common.py | 14 ++++++-------- tests/test_tmuxobject.py | 18 +++++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index 333edc748..aa23b53fa 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -388,9 +388,7 @@ class TmuxRelationalObject(Generic[O, D]): children: t.List[O] child_id_attribute: str - def find_where( - self, attrs: Dict[str, str] - ) -> Optional[Union["Pane", "Window", "Session"]]: + def find_where(self, attrs: D) -> Optional[Union["Pane", "Window", "Session"]]: """Return object on first match. .. versionchanged:: 0.4 @@ -403,18 +401,18 @@ def find_where( return None @overload - def where(self, attrs: Dict[str, str], first: "Literal[True]") -> O: + def where(self, attrs: D, first: "Literal[True]") -> O: ... @overload - def where(self, attrs: Dict[str, str], first: "Literal[False]") -> t.List[O]: + def where(self, attrs: D, first: "Literal[False]") -> t.List[O]: ... @overload - def where(self, attrs: Dict[str, str]) -> t.List[O]: + def where(self, attrs: D) -> t.List[O]: ... - def where(self, attrs: Dict[str, str], first: bool = False) -> t.Union[List[O], O]: + def where(self, attrs: D, first: bool = False) -> t.Union[List[O], O]: # ) -> List[Union["Session", "Pane", "Window", t.Any]]: """ Return objects matching child objects properties. @@ -445,7 +443,7 @@ def by(val: O) -> bool: return target_children[0] return target_children - def get_by_id(self, id: str) -> Optional[Union["Pane", "Window", "Session"]]: + def get_by_id(self, id: str) -> Optional[O]: """ Return object based on ``child_id_attribute``. diff --git a/tests/test_tmuxobject.py b/tests/test_tmuxobject.py index 98e53358e..992c73d3f 100644 --- a/tests/test_tmuxobject.py +++ b/tests/test_tmuxobject.py @@ -161,10 +161,10 @@ def test_get_by_id(server: Server, session: Session) -> None: for session in server.sessions: session_id = session.get("session_id") assert session_id is not None - get_by_id = server.get_by_id(session_id) + get_session_by_id = server.get_by_id(session_id) - assert get_by_id == session - assert isinstance(get_by_id, Session) + assert get_session_by_id == session + assert isinstance(get_session_by_id, Session) assert server.get_by_id("$" + next(namer)) is None # session.get_by_id @@ -172,10 +172,10 @@ def test_get_by_id(server: Server, session: Session) -> None: window_id = window.get("window_id") assert window_id is not None - get_by_id = session.get_by_id(window_id) + get_window_by_id = session.get_by_id(window_id) - assert get_by_id == window - assert isinstance(get_by_id, Window) + assert get_window_by_id == window + assert isinstance(get_window_by_id, Window) assert session.get_by_id("@" + next(namer)) is None @@ -184,8 +184,8 @@ def test_get_by_id(server: Server, session: Session) -> None: pane_id = pane.get("pane_id") assert pane_id is not None - get_by_id = window.get_by_id(pane_id) + get_pane_by_id = window.get_by_id(pane_id) - assert get_by_id == pane - assert isinstance(get_by_id, Pane) + assert get_pane_by_id == pane + assert isinstance(get_pane_by_id, Pane) assert window.get_by_id("%" + next(namer)) is None From 222a5e34eb456c46364473f001097ff22fca4e43 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Thu, 4 Aug 2022 22:08:08 -0500 Subject: [PATCH 2/3] chore: Remove unused comment --- libtmux/common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/libtmux/common.py b/libtmux/common.py index aa23b53fa..61399dbe9 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -413,7 +413,6 @@ def where(self, attrs: D) -> t.List[O]: ... def where(self, attrs: D, first: bool = False) -> t.Union[List[O], O]: - # ) -> List[Union["Session", "Pane", "Window", t.Any]]: """ Return objects matching child objects properties. From b20554c36d8ae36b3dbd18c7d7bd62569215e13f Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Thu, 4 Aug 2022 22:08:38 -0500 Subject: [PATCH 3/3] docs: Update return statement --- libtmux/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libtmux/common.py b/libtmux/common.py index 61399dbe9..b1660a79c 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -423,7 +423,7 @@ def where(self, attrs: D, first: bool = False) -> t.Union[List[O], O]: Returns ------- - list + list of objects, or one object if ``first=True`` """ # from https://github.com/serkanyersen/underscore.py