Skip to content

More typing improvements #392

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 3 commits into from
Aug 5, 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
17 changes: 7 additions & 10 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -403,19 +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]:
# ) -> List[Union["Session", "Pane", "Window", t.Any]]:
def where(self, attrs: D, first: bool = False) -> t.Union[List[O], O]:
"""
Return objects matching child objects properties.

Expand All @@ -426,7 +423,7 @@ def where(self, attrs: Dict[str, str], first: bool = False) -> t.Union[List[O],

Returns
-------
list
list of objects, or one object if ``first=True``
"""

# from https://github.com/serkanyersen/underscore.py
Expand All @@ -445,7 +442,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``.

Expand Down
18 changes: 9 additions & 9 deletions tests/test_tmuxobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,21 @@ 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
for window in session.windows:
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

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