Skip to content

Commit c83b07e

Browse files
committed
chore(ruff): Manual fixes
docs/conf.py:191:9: PERF203 `try`-`except` within a loop incurs performance overhead src/libtmux/_internal/query_list.py:62:9: TRY300 Consider moving this statement to an `else` block src/libtmux/_internal/query_list.py:297:19: TRY002 Create your own exception src/libtmux/_internal/query_list.py:297:19: TRY003 Avoid specifying long messages outside the exception class src/libtmux/_internal/query_list.py:337:25: TRY301 Abstract `raise` to an inner function src/libtmux/_internal/query_list.py:337:31: TRY003 Avoid specifying long messages outside the exception class src/libtmux/common.py:154:23: TRY003 Avoid specifying long messages outside the exception class src/libtmux/common.py:191:23: TRY003 Avoid specifying long messages outside the exception class src/libtmux/common.py:246:13: TRY400 Use `logging.exception` instead of `logging.error` src/libtmux/neo.py:243:15: TRY003 Avoid specifying long messages outside the exception class src/libtmux/pane.py:293:19: TRY003 Avoid specifying long messages outside the exception class src/libtmux/pane.py:439:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/pane.py:446:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/pytest_plugin.py:230:5: TRY302 Remove exception handler; error is immediately re-raised src/libtmux/server.py:146:13: TRY300 Consider moving this statement to an `else` block src/libtmux/server.py:210:23: TRY003 Avoid specifying long messages outside the exception class src/libtmux/server.py:241:13: TRY300 Consider moving this statement to an `else` block src/libtmux/server.py:515:17: PERF401 Use a list comprehension to create a transformed list src/libtmux/server.py:535:13: PERF401 Use a list comprehension to create a transformed list src/libtmux/server.py:553:13: PERF401 Use a list comprehension to create a transformed list src/libtmux/server.py:591:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:604:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:612:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:619:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:629:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:642:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:650:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:660:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:667:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:679:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/server.py:688:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:111:17: PERF401 Use a list comprehension to create a transformed list src/libtmux/session.py:130:17: PERF401 Use a list comprehension to create a transformed list src/libtmux/session.py:361:17: PERF401 Use a list comprehension to create a transformed list src/libtmux/session.py:366:19: TRY003 Avoid specifying long messages outside the exception class src/libtmux/session.py:373:19: TRY003 Avoid specifying long messages outside the exception class src/libtmux/session.py:487:31: PTH111 `os.path.expanduser()` should be replaced by `Path.expanduser()` src/libtmux/session.py:602:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:609:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:616:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:623:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:633:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:640:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:649:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:657:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/session.py:666:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/test.py:50:15: PTH100 `os.path.abspath()` should be replaced by `Path.resolve()` src/libtmux/test.py:50:31: PTH120 `os.path.dirname()` should be replaced by `Path.parent` src/libtmux/test.py:51:15: PTH100 `os.path.abspath()` should be replaced by `Path.resolve()` src/libtmux/test.py:51:31: PTH118 `os.path.join()` should be replaced by `Path` with `/` operator src/libtmux/test.py:52:33: PTH118 `os.path.join()` should be replaced by `Path` with `/` operator src/libtmux/window.py:123:17: PERF401 Use a list comprehension to create a transformed list src/libtmux/window.py:245:31: PTH111 `os.path.expanduser()` should be replaced by `Path.expanduser()` src/libtmux/window.py:473:13: TRY400 Use `logging.exception` instead of `logging.error` src/libtmux/window.py:633:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:640:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:647:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:654:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:664:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:671:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:680:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:688:9: B028 No explicit `stacklevel` keyword argument found src/libtmux/window.py:697:9: B028 No explicit `stacklevel` keyword argument found tests/legacy_api/test_common.py:34:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/legacy_api/test_common.py:54:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/legacy_api/test_common.py:70:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/legacy_api/test_common.py:87:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/legacy_api/test_server.py:152:10: B017 `pytest.raises(Exception)` should be considered evil tests/test_common.py:33:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/test_common.py:53:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/test_common.py:69:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/test_common.py:86:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` tests/test_server.py:169:10: B017 `pytest.raises(Exception)` should be considered evil tests/test_window.py:206:5: B018 Found useless expression. Either assign it to a variable or remove it.
1 parent fd54fa1 commit c83b07e

14 files changed

+124
-120
lines changed

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def linkcode_resolve(domain: str, info: t.Dict[str, str]) -> t.Union[None, str]:
188188
for part in fullname.split("."):
189189
try:
190190
obj = getattr(obj, part)
191-
except Exception:
191+
except Exception: # noqa: PERF203
192192
return None
193193

194194
# strip decorators, which would resolve to the source of the decorator

src/libtmux/common.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def show_environment(self) -> Dict[str, Union[bool, str]]:
151151
elif len(_t) == 1:
152152
vars_dict[_t[0]] = True
153153
else:
154-
raise ValueError(f"unexpected variable {_t}")
154+
raise exc.VariableUnpackingError(variable=_t)
155155

156156
return vars_dict
157157

@@ -188,7 +188,7 @@ def getenv(self, name: str) -> Optional[t.Union[str, bool]]:
188188
elif len(_t) == 1:
189189
vars_dict[_t[0]] = True
190190
else:
191-
raise ValueError(f"unexpected variable {_t}")
191+
raise exc.VariableUnpackingError(variable=_t)
192192

193193
return vars_dict.get(name)
194194

@@ -242,8 +242,8 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
242242
)
243243
stdout, stderr = self.process.communicate()
244244
returncode = self.process.returncode
245-
except Exception as e:
246-
logger.error(f"Exception for {subprocess.list2cmdline(cmd)}: \n{e}")
245+
except Exception:
246+
logger.exception(f"Exception for {subprocess.list2cmdline(cmd)}")
247247
raise
248248

249249
self.returncode = returncode

src/libtmux/neo.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import typing as t
44

55
from libtmux import exc
6-
from libtmux._internal.query_list import ObjectDoesNotExist
76
from libtmux.common import tmux_cmd
87
from libtmux.formats import FORMAT_SEPARATOR
98

@@ -240,9 +239,11 @@ def fetch_obj(
240239
obj = _obj
241240

242241
if obj is None:
243-
raise ObjectDoesNotExist(
244-
f"Could not find {obj_key}={obj_id} for {list_cmd} "
245-
f'{list_extra_args if list_extra_args is not None else ""}'
242+
raise exc.TmuxObjectDoesNotExist(
243+
obj_key=obj_key,
244+
obj_id=obj_id,
245+
list_cmd=list_cmd,
246+
list_extra_args=list_extra_args,
246247
)
247248

248249
assert obj is not None

src/libtmux/pane.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def select_pane(self) -> "Pane":
290290
assert isinstance(self.pane_id, str)
291291
pane = self.window.select_pane(self.pane_id)
292292
if pane is None:
293-
raise exc.LibTmuxException(f"Pane not found: {self}")
293+
raise exc.PaneNotFound(pane_id=self.pane_id)
294294
return pane
295295

296296
def split_window(
@@ -436,12 +436,12 @@ def get(self, key: str, default: t.Optional[t.Any] = None) -> t.Any:
436436
"""
437437
.. deprecated:: 0.16
438438
"""
439-
warnings.warn("Pane.get() is deprecated")
439+
warnings.warn("Pane.get() is deprecated", stacklevel=2)
440440
return getattr(self, key, default)
441441

442442
def __getitem__(self, key: str) -> t.Any:
443443
"""
444444
.. deprecated:: 0.16
445445
"""
446-
warnings.warn(f"Item lookups, e.g. pane['{key}'] is deprecated")
446+
warnings.warn(f"Item lookups, e.g. pane['{key}'] is deprecated", stacklevel=2)
447447
return getattr(self, key)

src/libtmux/pytest_plugin.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,7 @@ def session(
225225

226226
TEST_SESSION_NAME = get_test_session_name(server=server)
227227

228-
try:
229-
session = server.new_session(session_name=TEST_SESSION_NAME, **session_params)
230-
except exc.LibTmuxException:
231-
raise
228+
session = server.new_session(session_name=TEST_SESSION_NAME, **session_params)
232229

233230
"""
234231
Make sure that tmuxp can :ref:`test_builder_visually` and switches to

src/libtmux/server.py

+31-29
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ def is_alive(self) -> bool:
143143
"""
144144
try:
145145
res = self.cmd("list-sessions")
146-
return res.returncode == 0
147146
except Exception:
148147
return False
148+
return res.returncode == 0
149149

150150
def raise_if_dead(self) -> None:
151151
"""Raise if server not connected.
@@ -207,7 +207,7 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
207207
elif self.colors == 88:
208208
cmd_args.insert(0, "-8")
209209
else:
210-
raise ValueError("Server.colors must equal 88 or 256")
210+
raise exc.UnknownColorOption()
211211

212212
return tmux_cmd(*cmd_args, **kwargs)
213213

@@ -238,9 +238,9 @@ def attached_sessions(self) -> t.List[Session]:
238238
else:
239239
continue
240240

241-
return attached_sessions
242241
except Exception:
243242
return []
243+
return attached_sessions
244244

245245
def has_session(self, target_session: str, exact: bool = True) -> bool:
246246
"""
@@ -512,7 +512,7 @@ def sessions(self) -> QueryList[Session]: # type:ignore
512512
list_cmd="list-sessions",
513513
server=self,
514514
):
515-
sessions.append(Session(server=self, **obj))
515+
sessions.append(Session(server=self, **obj)) # noqa: PERF401
516516
except Exception:
517517
pass
518518

@@ -526,13 +526,14 @@ def windows(self) -> QueryList[Window]: # type:ignore
526526
:meth:`.windows.get() <libtmux._internal.query_list.QueryList.get()>` and
527527
:meth:`.windows.filter() <libtmux._internal.query_list.QueryList.filter()>`
528528
"""
529-
windows: t.List["Window"] = []
530-
for obj in fetch_objs(
531-
list_cmd="list-windows",
532-
list_extra_args=("-a",),
533-
server=self,
534-
):
535-
windows.append(Window(server=self, **obj))
529+
windows: t.List["Window"] = [
530+
Window(server=self, **obj)
531+
for obj in fetch_objs(
532+
list_cmd="list-windows",
533+
list_extra_args=("-a",),
534+
server=self,
535+
)
536+
]
536537

537538
return QueryList(windows)
538539

@@ -544,13 +545,14 @@ def panes(self) -> QueryList[Pane]: # type:ignore
544545
:meth:`.panes.get() <libtmux._internal.query_list.QueryList.get()>` and
545546
:meth:`.panes.filter() <libtmux._internal.query_list.QueryList.filter()>`
546547
"""
547-
panes: t.List["Pane"] = []
548-
for obj in fetch_objs(
549-
list_cmd="list-panes",
550-
list_extra_args=["-s"],
551-
server=self,
552-
):
553-
panes.append(Pane(server=self, **obj))
548+
panes: t.List["Pane"] = [
549+
Pane(server=self, **obj)
550+
for obj in fetch_objs(
551+
list_cmd="list-panes",
552+
list_extra_args=["-s"],
553+
server=self,
554+
)
555+
]
554556

555557
return QueryList(panes)
556558

@@ -588,7 +590,7 @@ def _list_panes(self) -> t.List[PaneDict]:
588590
589591
.. deprecated:: 0.16
590592
"""
591-
warnings.warn("Server._list_panes() is deprecated")
593+
warnings.warn("Server._list_panes() is deprecated", stacklevel=2)
592594
return [p.__dict__ for p in self.panes]
593595

594596
def _update_panes(self) -> "Server":
@@ -601,22 +603,22 @@ def _update_panes(self) -> "Server":
601603
602604
.. deprecated:: 0.16
603605
"""
604-
warnings.warn("Server._update_panes() is deprecated")
606+
warnings.warn("Server._update_panes() is deprecated", stacklevel=2)
605607
self._list_panes()
606608
return self
607609

608610
def get_by_id(self, id: str) -> t.Optional[Session]:
609611
"""
610612
.. deprecated:: 0.16
611613
"""
612-
warnings.warn("Server.get_by_id() is deprecated")
614+
warnings.warn("Server.get_by_id() is deprecated", stacklevel=2)
613615
return self.sessions.get(session_id=id, default=None)
614616

615617
def where(self, kwargs: t.Dict[str, t.Any]) -> t.List[Session]:
616618
"""
617619
.. deprecated:: 0.16
618620
"""
619-
warnings.warn("Server.find_where() is deprecated")
621+
warnings.warn("Server.find_where() is deprecated", stacklevel=2)
620622
try:
621623
return self.sessions.filter(**kwargs)
622624
except IndexError:
@@ -626,7 +628,7 @@ def find_where(self, kwargs: t.Dict[str, t.Any]) -> t.Optional[Session]:
626628
"""
627629
.. deprecated:: 0.16
628630
"""
629-
warnings.warn("Server.find_where() is deprecated")
631+
warnings.warn("Server.find_where() is deprecated", stacklevel=2)
630632
return self.sessions.get(default=None, **kwargs)
631633

632634
def _list_windows(self) -> t.List[WindowDict]:
@@ -639,15 +641,15 @@ def _list_windows(self) -> t.List[WindowDict]:
639641
640642
.. deprecated:: 0.16
641643
"""
642-
warnings.warn("Server._list_windows() is deprecated")
644+
warnings.warn("Server._list_windows() is deprecated", stacklevel=2)
643645
return [w.__dict__ for w in self.windows]
644646

645647
def _update_windows(self) -> "Server":
646648
"""Update internal window data and return ``self`` for chainability.
647649
648650
.. deprecated:: 0.16
649651
"""
650-
warnings.warn("Server._update_windows() is deprecated")
652+
warnings.warn("Server._update_windows() is deprecated", stacklevel=2)
651653
self._list_windows()
652654
return self
653655

@@ -657,14 +659,14 @@ def _sessions(self) -> t.List[SessionDict]:
657659
658660
.. deprecated:: 0.16
659661
"""
660-
warnings.warn("Server._sessions is deprecated")
662+
warnings.warn("Server._sessions is deprecated", stacklevel=2)
661663
return self._list_sessions()
662664

663665
def _list_sessions(self) -> t.List["SessionDict"]:
664666
"""
665667
.. deprecated:: 0.16
666668
"""
667-
warnings.warn("Server._list_sessions() is deprecated")
669+
warnings.warn("Server._list_sessions() is deprecated", stacklevel=2)
668670
return [s.__dict__ for s in self.sessions]
669671

670672
def list_sessions(self) -> t.List[Session]:
@@ -676,7 +678,7 @@ def list_sessions(self) -> t.List[Session]:
676678
-------
677679
list of :class:`Session`
678680
"""
679-
warnings.warn("Server.list_sessions is deprecated")
681+
warnings.warn("Server.list_sessions is deprecated", stacklevel=2)
680682
return self.sessions
681683

682684
@property
@@ -685,5 +687,5 @@ def children(self) -> QueryList["Session"]: # type:ignore
685687
686688
.. deprecated:: 0.16
687689
"""
688-
warnings.warn("Server.children is deprecated")
690+
warnings.warn("Server.children is deprecated", stacklevel=2)
689691
return self.sessions

0 commit comments

Comments
 (0)