Skip to content

Commit b92a4ff

Browse files
committed
snapshot.py(style[exceptions]): Fix linting issues identified by ruff
why: Improve code quality and maintainability by fixing linting issues. what: - Fixed Exception String Literal Issues (EM101) by extracting messages to variables - Fixed Line Length Issues (E501) by wrapping long lines with proper breaking - Fixed Exception Message Location Issues (TRY003) by restructuring exception raising - Fixed warnings.warn() calls by adding stacklevel=2 parameter (B028) - Formatted code with ruff format for consistent style Note: Left one PERF203 warning (try-except in loop) as is since it's specifically for doctest error handling and would require deeper refactoring.
1 parent 41296c7 commit b92a4ff

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

Diff for: src/libtmux/snapshot.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> None:
8181
NotImplementedError
8282
This method cannot be used on a snapshot.
8383
"""
84-
raise NotImplementedError(
85-
"Cannot execute commands on a snapshot. Use a real Pane object instead.",
84+
error_msg = (
85+
"Cannot execute commands on a snapshot. Use a real Pane object instead."
8686
)
87+
raise NotImplementedError(error_msg)
8788

8889
@property
8990
def content(self) -> list[str] | None:
@@ -92,8 +93,8 @@ def content(self) -> list[str] | None:
9293
Returns
9394
-------
9495
list[str] | None
95-
List of strings representing the content of the pane,
96-
or None if no content was captured.
96+
List of strings representing the content of the pane, or None if no
97+
content was captured.
9798
"""
9899
return self.pane_content
99100

@@ -112,12 +113,13 @@ def capture_pane(
112113
Returns
113114
-------
114115
list[str]
115-
List of strings representing the content of the pane, or empty list if no content
116-
was captured
116+
List of strings representing the content of the pane, or empty list if
117+
no content was captured
117118
118119
Notes
119120
-----
120-
This method is overridden to return the cached content instead of executing tmux commands.
121+
This method is overridden to return the cached content instead of executing
122+
tmux commands.
121123
"""
122124
if self.pane_content is None:
123125
return []
@@ -218,10 +220,11 @@ def from_pane(
218220

219221
# If all else fails, raise an error
220222
if source_server is None:
221-
raise ValueError(
223+
error_msg = (
222224
"Cannot create snapshot: pane has no server attribute "
223225
"and no window_snapshot provided"
224226
)
227+
raise ValueError(error_msg)
225228

226229
# Create a new instance
227230
snapshot = cls.__new__(cls)
@@ -275,9 +278,10 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> None:
275278
NotImplementedError
276279
This method cannot be used on a snapshot.
277280
"""
278-
raise NotImplementedError(
279-
"Cannot execute commands on a snapshot. Use a real Window object instead.",
281+
error_msg = (
282+
"Cannot execute commands on a snapshot. Use a real Window object instead."
280283
)
284+
raise NotImplementedError(error_msg)
281285

282286
@property
283287
def panes(self) -> QueryList[PaneSnapshot]:
@@ -357,10 +361,11 @@ def from_window(
357361

358362
# If all else fails, raise an error
359363
if source_server is None:
360-
raise ValueError(
364+
error_msg = (
361365
"Cannot create snapshot: window has no server attribute "
362366
"and no session_snapshot provided"
363367
)
368+
raise ValueError(error_msg)
364369

365370
# Create a new instance
366371
snapshot = cls.__new__(cls)
@@ -426,9 +431,10 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> None:
426431
NotImplementedError
427432
This method cannot be used on a snapshot.
428433
"""
429-
raise NotImplementedError(
430-
"Cannot execute commands on a snapshot. Use a real Session object instead.",
434+
error_msg = (
435+
"Cannot execute commands on a snapshot. Use a real Session object instead."
431436
)
437+
raise NotImplementedError(error_msg)
432438

433439
@property
434440
def windows(self) -> QueryList[WindowSnapshot]:
@@ -502,10 +508,11 @@ def from_session(
502508

503509
# If all else fails, raise an error
504510
if source_server is None:
505-
raise ValueError(
511+
error_msg = (
506512
"Cannot create snapshot: session has no server attribute "
507513
"and no server_snapshot provided"
508514
)
515+
raise ValueError(error_msg)
509516

510517
# Create a new instance
511518
snapshot = cls.__new__(cls)
@@ -579,9 +586,10 @@ def cmd(self, cmd: str, *args: t.Any, **kwargs: t.Any) -> None:
579586
NotImplementedError
580587
This method cannot be used on a snapshot.
581588
"""
582-
raise NotImplementedError(
583-
"Cannot execute commands on a snapshot. Use a real Server object instead.",
589+
error_msg = (
590+
"Cannot execute commands on a snapshot. Use a real Server object instead."
584591
)
592+
raise NotImplementedError(error_msg)
585593

586594
@property
587595
def sessions(self) -> QueryList[SessionSnapshot]:
@@ -619,7 +627,8 @@ def raise_if_dead(self) -> None:
619627
ConnectionError
620628
Always raised since snapshots are not connected to a live tmux server
621629
"""
622-
raise ConnectionError("ServerSnapshot is not connected to a live tmux server")
630+
error_msg = "ServerSnapshot is not connected to a live tmux server"
631+
raise ConnectionError(error_msg)
623632

624633
@classmethod
625634
def from_server(
@@ -676,7 +685,10 @@ def from_server(
676685
if "test" in sys.modules:
677686
import warnings
678687

679-
warnings.warn(f"Failed to create session snapshot: {e}")
688+
warnings.warn(
689+
f"Failed to create session snapshot: {e}",
690+
stacklevel=2,
691+
)
680692
continue
681693
else:
682694
raise

0 commit comments

Comments
 (0)