Skip to content

Commit fd457a0

Browse files
committed
libtmux(fix[logging]): guard None/int values in structured extra keys
why: Extra keys with None or int values violate the AGENTS.md schema requiring str-typed scalar values. session_name, session_id, pane_id are all str | None per neo.py dataclass definitions. what: - session.py new_window: use conditional extra pattern for nullable fields - session.py kill_window: fix unbound target, use session_name not window_name, accept str | int | None type for target_window - session.py kill: wrap session_name and session_id in None guards - pane.py split: use conditional extra pattern for nullable fields - pane.py kill: wrap pane_id in None guard
1 parent 0436208 commit fd457a0

2 files changed

Lines changed: 51 additions & 38 deletions

File tree

src/libtmux/pane.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,13 @@ def kill(
567567
if proc.stderr:
568568
raise exc.LibTmuxException(proc.stderr)
569569

570-
logger.info(
571-
"pane killed",
572-
extra={
573-
"tmux_subcommand": "kill-pane",
574-
"tmux_pane": self.pane_id,
575-
"tmux_target": self.pane_id,
576-
},
577-
)
570+
extra: dict[str, str] = {
571+
"tmux_subcommand": "kill-pane",
572+
}
573+
if self.pane_id is not None:
574+
extra["tmux_pane"] = str(self.pane_id)
575+
extra["tmux_target"] = str(self.pane_id)
576+
logger.info("pane killed", extra=extra)
578577

579578
"""
580579
Commands ("climber"-helpers)
@@ -780,16 +779,18 @@ def split(
780779

781780
pane = self.from_pane_id(server=self.server, pane_id=pane_formatters["pane_id"])
782781

783-
logger.info(
784-
"pane created",
785-
extra={
786-
"tmux_subcommand": "split-window",
787-
"tmux_session": self.session.session_name,
788-
"tmux_window": self.window.window_name,
789-
"tmux_pane": pane.pane_id,
790-
"tmux_target": target,
791-
},
792-
)
782+
extra: dict[str, str] = {
783+
"tmux_subcommand": "split-window",
784+
"tmux_pane": str(pane.pane_id),
785+
}
786+
if self.session.session_name is not None:
787+
extra["tmux_session"] = str(self.session.session_name)
788+
if self.window.window_name is not None:
789+
extra["tmux_window"] = str(self.window.window_name)
790+
if target is not None:
791+
extra["tmux_target"] = str(target)
792+
793+
logger.info("pane created", extra=extra)
793794

794795
return pane
795796

src/libtmux/session.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,14 @@ def kill(
395395
if proc.stderr:
396396
raise exc.LibTmuxException(proc.stderr)
397397

398-
logger.info(
399-
"session killed",
400-
extra={
401-
"tmux_subcommand": "kill-session",
402-
"tmux_session": self.session_name,
403-
"tmux_target": self.session_id,
404-
},
405-
)
398+
extra: dict[str, str] = {
399+
"tmux_subcommand": "kill-session",
400+
}
401+
if self.session_name is not None:
402+
extra["tmux_session"] = str(self.session_name)
403+
if self.session_id is not None:
404+
extra["tmux_target"] = str(self.session_id)
405+
logger.info("session killed", extra=extra)
406406

407407
def switch_client(self) -> Session:
408408
"""Switch client to session.
@@ -584,19 +584,21 @@ def new_window(
584584
window_id=window_formatters["window_id"],
585585
)
586586

587-
logger.info(
588-
"window created",
589-
extra={
590-
"tmux_subcommand": "new-window",
591-
"tmux_session": self.session_name,
592-
"tmux_window": window_name,
593-
"tmux_target": target,
594-
},
595-
)
587+
extra: dict[str, str] = {
588+
"tmux_subcommand": "new-window",
589+
}
590+
if self.session_name is not None:
591+
extra["tmux_session"] = str(self.session_name)
592+
if window.window_name is not None:
593+
extra["tmux_window"] = str(window.window_name)
594+
if target is not None:
595+
extra["tmux_target"] = str(target)
596+
597+
logger.info("window created", extra=extra)
596598

597599
return window
598600

599-
def kill_window(self, target_window: str | None = None) -> None:
601+
def kill_window(self, target_window: str | int | None = None) -> None:
600602
"""Close a tmux window, and all panes inside it, ``$ tmux kill-window``.
601603
602604
Kill the current window or the window at ``target-window``. removing it
@@ -612,9 +614,10 @@ def kill_window(self, target_window: str | None = None) -> None:
612614
:exc:`libtmux.exc.LibTmuxException`
613615
If tmux returns an error.
614616
"""
615-
if target_window:
617+
target: str | int | None = target_window
618+
if target_window is not None:
616619
if isinstance(target_window, int):
617-
target = f"{self.window_name}:{target_window}"
620+
target = f"{self.session_name}:{target_window}"
618621
else:
619622
target = f"{target_window}"
620623

@@ -623,6 +626,15 @@ def kill_window(self, target_window: str | None = None) -> None:
623626
if proc.stderr:
624627
raise exc.LibTmuxException(proc.stderr)
625628

629+
extra: dict[str, str] = {
630+
"tmux_subcommand": "kill-window",
631+
}
632+
if self.session_name is not None:
633+
extra["tmux_session"] = str(self.session_name)
634+
if target is not None:
635+
extra["tmux_target"] = str(target)
636+
logger.info("window killed", extra=extra)
637+
626638
#
627639
# Dunder
628640
#

0 commit comments

Comments
 (0)