Skip to content

Commit 162c67a

Browse files
committed
src/libtmux/snapshot.py uv run ruff check --select ALL src/libtmux/snapshot.py tests/test_snapshot.py --fix --unsafe-fixes --preview --show-fixes; uv run ruff format .
1 parent 5d9eeaa commit 162c67a

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

src/libtmux/snapshot.py

+48-34
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import typing as t
1616
from dataclasses import field
1717
from datetime import datetime
18-
from types import TracebackType
18+
19+
from typing_extensions import Self
1920

2021
from libtmux._internal.frozen_dataclass_sealable import frozen_dataclass_sealable
2122
from libtmux._internal.query_list import QueryList
@@ -25,7 +26,7 @@
2526
from libtmux.window import Window
2627

2728
if t.TYPE_CHECKING:
28-
pass
29+
from types import TracebackType
2930

3031

3132
@frozen_dataclass_sealable
@@ -39,10 +40,11 @@ class PaneSnapshot(Pane):
3940
pane_content: list[str] | None = None
4041
created_at: datetime = field(default_factory=datetime.now)
4142
window_snapshot: WindowSnapshot | None = field(
42-
default=None, metadata={"mutable_during_init": True}
43+
default=None,
44+
metadata={"mutable_during_init": True},
4345
)
4446

45-
def __enter__(self) -> PaneSnapshot:
47+
def __enter__(self) -> Self:
4648
"""Context manager entry point."""
4749
return self
4850

@@ -53,7 +55,6 @@ def __exit__(
5355
exc_tb: TracebackType | None,
5456
) -> None:
5557
"""Context manager exit point."""
56-
pass
5758

5859
def cmd(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn:
5960
"""Prevent executing tmux commands on a snapshot."""
@@ -136,13 +137,15 @@ class WindowSnapshot(Window):
136137
# Fields only present in snapshot
137138
created_at: datetime = field(default_factory=datetime.now)
138139
session_snapshot: SessionSnapshot | None = field(
139-
default=None, metadata={"mutable_during_init": True}
140+
default=None,
141+
metadata={"mutable_during_init": True},
140142
)
141143
panes_snapshot: list[PaneSnapshot] = field(
142-
default_factory=list, metadata={"mutable_during_init": True}
144+
default_factory=list,
145+
metadata={"mutable_during_init": True},
143146
)
144147

145-
def __enter__(self) -> WindowSnapshot:
148+
def __enter__(self) -> Self:
146149
"""Context manager entry point."""
147150
return self
148151

@@ -153,7 +156,6 @@ def __exit__(
153156
exc_tb: TracebackType | None,
154157
) -> None:
155158
"""Context manager exit point."""
156-
pass
157159

158160
def cmd(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn:
159161
"""Prevent executing tmux commands on a snapshot."""
@@ -217,7 +219,9 @@ def from_window(
217219
panes_snapshot = []
218220
for pane in window.panes:
219221
pane_snapshot = PaneSnapshot.from_pane(
220-
pane, capture_content=capture_content, window_snapshot=snapshot
222+
pane,
223+
capture_content=capture_content,
224+
window_snapshot=snapshot,
221225
)
222226
panes_snapshot.append(pane_snapshot)
223227
object.__setattr__(snapshot, "panes_snapshot", panes_snapshot)
@@ -238,13 +242,15 @@ class SessionSnapshot(Session):
238242
# Fields only present in snapshot
239243
created_at: datetime = field(default_factory=datetime.now)
240244
server_snapshot: ServerSnapshot | None = field(
241-
default=None, metadata={"mutable_during_init": True}
245+
default=None,
246+
metadata={"mutable_during_init": True},
242247
)
243248
windows_snapshot: list[WindowSnapshot] = field(
244-
default_factory=list, metadata={"mutable_during_init": True}
249+
default_factory=list,
250+
metadata={"mutable_during_init": True},
245251
)
246252

247-
def __enter__(self) -> SessionSnapshot:
253+
def __enter__(self) -> Self:
248254
"""Context manager entry point."""
249255
return self
250256

@@ -255,7 +261,6 @@ def __exit__(
255261
exc_tb: TracebackType | None,
256262
) -> None:
257263
"""Context manager exit point."""
258-
pass
259264

260265
def cmd(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn:
261266
"""Prevent executing tmux commands on a snapshot."""
@@ -326,7 +331,9 @@ def from_session(
326331
windows_snapshot = []
327332
for window in session.windows:
328333
window_snapshot = WindowSnapshot.from_window(
329-
window, capture_content=capture_content, session_snapshot=snapshot
334+
window,
335+
capture_content=capture_content,
336+
session_snapshot=snapshot,
330337
)
331338
windows_snapshot.append(window_snapshot)
332339
object.__setattr__(snapshot, "windows_snapshot", windows_snapshot)
@@ -347,16 +354,19 @@ class ServerSnapshot(Server):
347354
# Fields only present in snapshot
348355
created_at: datetime = field(default_factory=datetime.now)
349356
sessions_snapshot: list[SessionSnapshot] = field(
350-
default_factory=list, metadata={"mutable_during_init": True}
357+
default_factory=list,
358+
metadata={"mutable_during_init": True},
351359
)
352360
windows_snapshot: list[WindowSnapshot] = field(
353-
default_factory=list, metadata={"mutable_during_init": True}
361+
default_factory=list,
362+
metadata={"mutable_during_init": True},
354363
)
355364
panes_snapshot: list[PaneSnapshot] = field(
356-
default_factory=list, metadata={"mutable_during_init": True}
365+
default_factory=list,
366+
metadata={"mutable_during_init": True},
357367
)
358368

359-
def __enter__(self) -> ServerSnapshot:
369+
def __enter__(self) -> Self:
360370
"""Context manager entry point."""
361371
return self
362372

@@ -367,7 +377,6 @@ def __exit__(
367377
exc_tb: TracebackType | None,
368378
) -> None:
369379
"""Context manager exit point."""
370-
pass
371380

372381
def cmd(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn:
373382
"""Prevent executing tmux commands on a snapshot."""
@@ -400,7 +409,9 @@ def panes(self) -> QueryList[PaneSnapshot]:
400409

401410
@classmethod
402411
def from_server(
403-
cls, server: Server, include_content: bool = True
412+
cls,
413+
server: Server,
414+
include_content: bool = True,
404415
) -> ServerSnapshot:
405416
"""Create a ServerSnapshot from a live Server.
406417
@@ -430,11 +441,11 @@ def from_server(
430441

431442
# Copy server attributes
432443
for name, value in vars(server).items():
433-
if not name.startswith("_") and name not in [
444+
if not name.startswith("_") and name not in {
434445
"sessions",
435446
"windows",
436447
"panes",
437-
]:
448+
}:
438449
object.__setattr__(snapshot, name, copy.deepcopy(value))
439450

440451
# Set snapshot-specific fields
@@ -474,7 +485,8 @@ def from_server(
474485
def filter_snapshot(
475486
snapshot: ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot,
476487
filter_func: t.Callable[
477-
[ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot], bool
488+
[ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot],
489+
bool,
478490
],
479491
) -> ServerSnapshot | SessionSnapshot | WindowSnapshot | PaneSnapshot | None:
480492
"""Filter a snapshot hierarchy based on a filter function.
@@ -525,7 +537,7 @@ def filter_snapshot(
525537
return server_copy
526538

527539
# Handle filtering SessionSnapshot
528-
elif isinstance(snapshot, SessionSnapshot):
540+
if isinstance(snapshot, SessionSnapshot):
529541
filtered_windows = []
530542

531543
# Filter each window
@@ -544,7 +556,7 @@ def filter_snapshot(
544556
return session_copy
545557

546558
# Handle filtering WindowSnapshot
547-
elif isinstance(snapshot, WindowSnapshot):
559+
if isinstance(snapshot, WindowSnapshot):
548560
filtered_panes = []
549561

550562
# Filter each pane - panes are leaf nodes
@@ -560,7 +572,7 @@ def filter_snapshot(
560572
return window_copy
561573

562574
# Handle filtering PaneSnapshot (leaf node)
563-
elif isinstance(snapshot, PaneSnapshot):
575+
if isinstance(snapshot, PaneSnapshot):
564576
if filter_func(snapshot):
565577
return snapshot
566578
return None
@@ -588,22 +600,23 @@ def snapshot_to_dict(
588600
"""
589601
# Base case: For non-snapshot objects, just return them directly
590602
if not isinstance(
591-
snapshot, (ServerSnapshot, SessionSnapshot, WindowSnapshot, PaneSnapshot)
603+
snapshot,
604+
(ServerSnapshot, SessionSnapshot, WindowSnapshot, PaneSnapshot),
592605
):
593-
return t.cast(dict[str, t.Any], snapshot)
606+
return t.cast("dict[str, t.Any]", snapshot)
594607

595608
# Convert dataclass to dict
596609
result: dict[str, t.Any] = {}
597610

598611
# Get all fields from the instance
599612
for name, value in vars(snapshot).items():
600613
# Skip internal and parent reference fields - we want a tree, not a graph with cycles
601-
if name.startswith("_") or name in [
614+
if name.startswith("_") or name in {
602615
"server",
603616
"server_snapshot",
604617
"session_snapshot",
605618
"window_snapshot",
606-
]:
619+
}:
607620
continue
608621

609622
# Handle lists of snapshots
@@ -618,7 +631,8 @@ def snapshot_to_dict(
618631
result[name] = [snapshot_to_dict(item) for item in value]
619632
# Handle nested snapshots
620633
elif isinstance(
621-
value, (ServerSnapshot, SessionSnapshot, WindowSnapshot, PaneSnapshot)
634+
value,
635+
(ServerSnapshot, SessionSnapshot, WindowSnapshot, PaneSnapshot),
622636
):
623637
result[name] = snapshot_to_dict(value)
624638
# Handle QueryList (convert to regular list first)
@@ -670,7 +684,7 @@ def is_active(
670684
"""Return True if the object is active."""
671685
if isinstance(obj, PaneSnapshot):
672686
return getattr(obj, "pane_active", "0") == "1"
673-
elif isinstance(obj, WindowSnapshot):
687+
if isinstance(obj, WindowSnapshot):
674688
return getattr(obj, "window_active", "0") == "1"
675689
# Servers and sessions are always considered active
676690
return isinstance(obj, (ServerSnapshot, SessionSnapshot))
@@ -679,4 +693,4 @@ def is_active(
679693
if filtered is None:
680694
error_msg = "No active objects found!"
681695
raise ValueError(error_msg)
682-
return t.cast(ServerSnapshot, filtered)
696+
return t.cast("ServerSnapshot", filtered)

0 commit comments

Comments
 (0)