Skip to content

Commit

Permalink
ci(ruff): Automated lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Jul 22, 2023
1 parent 6720e45 commit ec1fa4e
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 63 deletions.
1 change: 0 additions & 1 deletion conftest.py
Expand Up @@ -12,7 +12,6 @@
import typing as t

import pytest

from _pytest.doctest import DoctestItem

from libtmux.pytest_plugin import USING_ZSH
Expand Down
6 changes: 3 additions & 3 deletions docs/conf.py
Expand Up @@ -6,8 +6,8 @@
import pathlib
import typing as t

import libtmux # NOQA
from libtmux import test # NOQA
import libtmux
from libtmux import test

if t.TYPE_CHECKING:
from sphinx.application import Sphinx
Expand Down Expand Up @@ -168,7 +168,7 @@

def linkcode_resolve(
domain: str, info: t.Dict[str, str]
) -> t.Union[None, str]: # NOQA: C901
) -> t.Union[None, str]:
"""
Determine the URL corresponding to Python object
Expand Down
8 changes: 1 addition & 7 deletions src/libtmux/_internal/query_list.py
Expand Up @@ -123,7 +123,7 @@ def lookup_icontains(
if isinstance(data, str):
return rhs.lower() in data.lower()
if isinstance(data, Mapping):
return rhs.lower() in [k.lower() for k in data.keys()]
return rhs.lower() in [k.lower() for k in data]

return False

Expand Down Expand Up @@ -183,7 +183,6 @@ def lookup_in(
return rhs in data
# TODO: Add a deep Mappingionary matcher
# if isinstance(rhs, Mapping) and isinstance(data, Mapping):
# return rhs.items() not in data.items()
except Exception:
return False
return False
Expand All @@ -205,7 +204,6 @@ def lookup_nin(
return rhs not in data
# TODO: Add a deep Mappingionary matcher
# if isinstance(rhs, Mapping) and isinstance(data, Mapping):
# return rhs.items() not in data.items()
except Exception:
return False
return False
Expand Down Expand Up @@ -293,10 +291,6 @@ def __eq__(
self,
other: object,
# other: Union[
# "QueryList[T]",
# List[Mapping[str, str]],
# List[Mapping[str, int]],
# List[Mapping[str, Union[str, Mapping[str, Union[List[str], str]]]]],
# ],
) -> bool:
data = other
Expand Down
2 changes: 0 additions & 2 deletions src/libtmux/formats.py
Expand Up @@ -41,14 +41,12 @@
]

WINDOW_FORMATS = [
# format_window()
"window_id",
"window_name",
"window_width",
"window_height",
"window_layout",
"window_panes",
# format_winlink()
"window_index",
"window_flags",
"window_active",
Expand Down
9 changes: 3 additions & 6 deletions src/libtmux/neo.py
Expand Up @@ -40,16 +40,13 @@ class Obj:
alternate_saved_x: t.Union[str, None] = None
alternate_saved_y: t.Union[str, None] = None
# See QUIRK_TMUX_3_1_X_0001
# buffer_created: t.Union[str, None] = None
buffer_name: t.Union[str, None] = None
buffer_sample: t.Union[str, None] = None
buffer_size: t.Union[str, None] = None
# See QUIRK_TMUX_3_1_X_0001
# client_activity: t.Union[str, None] = None
client_cell_height: t.Union[str, None] = None
client_cell_width: t.Union[str, None] = None
# See QUIRK_TMUX_3_1_X_0001
# client_created: t.Union[str, None] = None
client_discarded: t.Union[str, None] = None
client_flags: t.Union[str, None] = None
client_height: t.Union[str, None] = None
Expand Down Expand Up @@ -182,11 +179,11 @@ def _refresh(


def fetch_objs(
server: "Server", list_cmd: "ListCmd", list_extra_args: "ListExtraArgs" = None
server: "Server", list_cmd: "ListCmd", list_extra_args: "t.Optional[ListExtraArgs]" = None
) -> OutputsRaw:
formats = list(Obj.__dataclass_fields__.keys())

cmd_args: t.List[t.Union[str, int]] = list()
cmd_args: t.List[t.Union[str, int]] = []

if server.socket_name:
cmd_args.insert(0, f"-L{server.socket_name}")
Expand Down Expand Up @@ -229,7 +226,7 @@ def fetch_obj(
obj_key: str,
obj_id: str,
list_cmd: "ListCmd" = "list-panes",
list_extra_args: "ListExtraArgs" = None,
list_extra_args: "t.Optional[ListExtraArgs]" = None,
) -> OutputRaw:
obj_formatters_filtered = fetch_objs(
server=server, list_cmd=list_cmd, list_extra_args=list_extra_args
Expand Down
11 changes: 5 additions & 6 deletions src/libtmux/pytest_plugin.py
@@ -1,3 +1,4 @@
import contextlib
import getpass
import logging
import os
Expand Down Expand Up @@ -80,7 +81,7 @@ def clear_env(monkeypatch: pytest.MonkeyPatch) -> None:
tmux show-environment tests were being interrupted due to a lot of crazy env vars.
"""
for k, v in os.environ.items():
for k, _v in os.environ.items():
if not any(
needle in k.lower()
for needle in [
Expand Down Expand Up @@ -236,16 +237,14 @@ def session(
session_id = session.session_id
assert session_id is not None

try:
with contextlib.suppress(exc.LibTmuxException):
server.switch_client(target_session=session_id)
except exc.LibTmuxException:
# server.attach_session(session.get('session_id'))
pass


for old_test_session in old_test_sessions:
logger.debug(f"Old test test session {old_test_session} found. Killing it.")
server.kill_session(old_test_session)
assert TEST_SESSION_NAME == session.session_name
assert session.session_name == TEST_SESSION_NAME
assert TEST_SESSION_NAME != "tmuxp"

return session
9 changes: 4 additions & 5 deletions src/libtmux/server.py
Expand Up @@ -169,7 +169,7 @@ def raise_if_dead(self) -> None:
if self.config_file:
cmd_args.insert(0, f"-f{self.config_file}")

subprocess.check_call([tmux_bin] + cmd_args)
subprocess.check_call([tmux_bin, *cmd_args])

#
# Command
Expand Down Expand Up @@ -227,7 +227,7 @@ def attached_sessions(self) -> t.List[Session]:
"""
try:
sessions = self.sessions
attached_sessions = list()
attached_sessions = []

for session in sessions:
attached = session.session_attached
Expand All @@ -239,7 +239,6 @@ def attached_sessions(self) -> t.List[Session]:
continue

return attached_sessions
# return [Session(**s) for s in attached_sessions] or None
except Exception:
return []

Expand Down Expand Up @@ -339,7 +338,7 @@ def attach_session(self, target_session: t.Optional[str] = None) -> None:
"""
session_check_name(target_session)

tmux_args: t.Tuple[str, ...] = tuple()
tmux_args: t.Tuple[str, ...] = ()
if target_session:
tmux_args += ("-t%s" % target_session,)

Expand Down Expand Up @@ -574,7 +573,7 @@ def __repr__(self) -> str:
elif self.socket_path is not None:
return (
f"{self.__class__.__name__}"
f"(socket_path={getattr(self, 'socket_path')})"
f"(socket_path={self.socket_path})"
)
return f"{self.__class__.__name__}" f"(socket_path=/tmp/tmux-1000/default)"

Expand Down
14 changes: 6 additions & 8 deletions src/libtmux/session.py
Expand Up @@ -151,7 +151,7 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
# if -t is not set in any arg yet
if not any("-t" in str(x) for x in args):
# insert -t immediately after 1st arg, as per tmux format
new_args: t.Tuple[str, ...] = tuple()
new_args: t.Tuple[str, ...] = ()
new_args += (args[0],)
assert isinstance(self.session_id, str)
new_args += (
Expand All @@ -163,7 +163,6 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
return self.server.cmd(*args, **kwargs)

#
# Commands (tmux-like)
#
def set_option(
self, option: str, value: t.Union[str, int], _global: bool = False
Expand Down Expand Up @@ -197,7 +196,7 @@ def set_option(
elif isinstance(value, bool) and not value:
value = "off"

tmux_args: t.Tuple[t.Union[str, int], ...] = tuple()
tmux_args: t.Tuple[t.Union[str, int], ...] = ()

if _global:
tmux_args += ("-g",)
Expand Down Expand Up @@ -240,7 +239,7 @@ def show_options(
Uses ``_global`` for keyword name instead of ``global`` to avoid
colliding with reserved keyword.
"""
tmux_args: t.Tuple[str, ...] = tuple()
tmux_args: t.Tuple[str, ...] = ()

if _global:
tmux_args += ("-g",)
Expand Down Expand Up @@ -288,7 +287,7 @@ def show_option(
Test and return True/False for on/off string.
"""

tmux_args: t.Tuple[str, ...] = tuple()
tmux_args: t.Tuple[str, ...] = ()

if _global:
tmux_args += ("-g",)
Expand Down Expand Up @@ -476,7 +475,7 @@ def new_window(
-------
:class:`Window`
"""
window_args: t.Tuple[str, ...] = tuple()
window_args: t.Tuple[str, ...] = ()

if not attach:
window_args += ("-d",)
Expand All @@ -494,8 +493,7 @@ def new_window(

window_args += (
# empty string for window_index will use the first one available
"-t%s:%s"
% (self.session_id, window_index),
f"-t{self.session_id}:{window_index}",
)

if environment:
Expand Down
2 changes: 1 addition & 1 deletion src/libtmux/test.py
Expand Up @@ -305,7 +305,7 @@ class EnvironmentVarGuard:
def __init__(self) -> None:
self._environ = os.environ
self._unset: t.Set[str] = set()
self._reset: t.Dict[str, str] = dict()
self._reset: t.Dict[str, str] = {}

def set(self, envvar: str, value: str) -> None:
if envvar not in self._environ:
Expand Down
25 changes: 10 additions & 15 deletions src/libtmux/window.py
Expand Up @@ -125,7 +125,6 @@ def panes(self) -> QueryList["Pane"]: # type: ignore
return QueryList(panes)

#
# Command (pane-scoped)
#
def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
"""Return :meth:`Server.cmd` defaulting to ``target_window`` as target.
Expand All @@ -136,12 +135,11 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
``args`` will override using the object's ``window_id`` as target.
"""
if not any(arg.startswith("-t") for arg in args):
args = ("-t", self.window_id) + args
args = ("-t", self.window_id, *args)

return self.server.cmd(cmd, *args, **kwargs)

#
# Commands (tmux-like)
#
def select_pane(self, target_pane: t.Union[str, int]) -> t.Optional["Pane"]:
"""
Expand Down Expand Up @@ -219,21 +217,18 @@ def split_window(
"""
tmux_formats = ["#{pane_id}" + FORMAT_SEPARATOR]

# '-t%s' % self.attached_pane.get('pane_id'),
# 2013-10-18 LOOK AT THIS, rm'd it..
tmux_args: t.Tuple[str, ...] = tuple()
tmux_args: t.Tuple[str, ...] = ()

if target is not None:
tmux_args += ("-t%s" % target,)
else:
if len(self.panes):
tmux_args += (
"-t%s:%s.%s"
% (self.session_id, self.window_id, self.panes[0].pane_index),
f"-t{self.session_id}:{self.window_id}.{self.panes[0].pane_index}",
)
else:
tmux_args += ("-t%s:%s" % (self.session_id, self.window_id),)
# tmux_args += ("-t%s" % self.panes[0].pane_id,)
tmux_args += (f"-t{self.session_id}:{self.window_id}",)

if vertical:
tmux_args += ("-v",)
Expand Down Expand Up @@ -313,7 +308,7 @@ def select_layout(self, layout: t.Optional[str] = None) -> "Window":
'custom'
custom dimensions (see :term:`tmux(1)` manpages).
"""
cmd = ["select-layout", "-t{}:{}".format(self.session_id, self.window_index)]
cmd = ["select-layout", f"-t{self.session_id}:{self.window_index}"]

if layout: # tmux allows select-layout without args
cmd.append(layout)
Expand Down Expand Up @@ -349,7 +344,7 @@ def set_window_option(self, option: str, value: t.Union[int, str]) -> "Window":

cmd = self.cmd(
"set-window-option",
"-t{}:{}".format(self.session_id, self.window_index),
f"-t{self.session_id}:{self.window_index}",
option,
value,
)
Expand All @@ -375,7 +370,7 @@ def show_window_options(self, g: t.Optional[bool] = False) -> "WindowOptionDict"
g : str, optional
Pass ``-g`` flag for global variable, default False.
"""
tmux_args: t.Tuple[str, ...] = tuple()
tmux_args: t.Tuple[str, ...] = ()

if g:
tmux_args += ("-g",)
Expand Down Expand Up @@ -419,7 +414,7 @@ def show_window_option(
:exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
:exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
"""
tmux_args: t.Tuple[t.Union[str, int], ...] = tuple()
tmux_args: t.Tuple[t.Union[str, int], ...] = ()

if g:
tmux_args += ("-g",)
Expand Down Expand Up @@ -486,7 +481,7 @@ def kill_window(self) -> None:

proc = self.cmd(
"kill-window",
"-t{}:{}".format(self.session_id, self.window_index),
f"-t{self.session_id}:{self.window_index}",
)

if proc.stderr:
Expand All @@ -510,7 +505,7 @@ def move_window(
session = session or self.session_id
proc = self.cmd(
"move-window",
"-s{}:{}".format(self.session_id, self.window_index),
f"-s{self.session_id}:{self.window_index}",
f"-t{session}:{destination}",
)

Expand Down
2 changes: 1 addition & 1 deletion tests/legacy_api/test_common.py
Expand Up @@ -62,7 +62,7 @@ def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi:
assert has_minimum_version()
assert has_gte_version(TMUX_MIN_VERSION)
assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version"
assert TMUX_NEXT_VERSION == get_version()
assert get_version() == TMUX_NEXT_VERSION


def test_get_version_openbsd(monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/legacy_api/test_server.py
Expand Up @@ -71,12 +71,12 @@ def test_show_environment(server: Server) -> None:
def test_getenv(server: Server, session: Session) -> None:
"""Set environment then Server.show_environment(key)."""
server.set_environment("FOO", "BAR")
assert "BAR" == server.getenv("FOO")
assert server.getenv("FOO") == "BAR"

server.set_environment("FOO", "DAR")
assert "DAR" == server.getenv("FOO")
assert server.getenv("FOO") == "DAR"

assert "DAR" == server.show_environment()["FOO"]
assert server.show_environment()["FOO"] == "DAR"


def test_show_environment_not_set(server: Server) -> None:
Expand Down

0 comments on commit ec1fa4e

Please sign in to comment.