Skip to content

Commit 5949b65

Browse files
committed
Session(refactor): Replace deprecation warnings with DeprecatedError
why: Enforce immediate migration from deprecated APIs. what: - attached_window raises DeprecatedError, use active_window instead - attached_pane raises DeprecatedError, use active_pane instead - attach_session() raises DeprecatedError, use attach() instead - kill_session() raises DeprecatedError, use kill() instead - get(), __getitem__() raise DeprecatedError, use attribute access - get_by_id(), where(), find_where() raise DeprecatedError - list_windows(), _windows, _list_windows(), children raise DeprecatedError
1 parent 8e7a631 commit 5949b65

File tree

1 file changed

+52
-78
lines changed

1 file changed

+52
-78
lines changed

src/libtmux/session.py

Lines changed: 52 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import logging
1212
import pathlib
1313
import typing as t
14-
import warnings
1514

1615
from libtmux._internal.query_list import QueryList
1716
from libtmux.common import tmux_cmd
@@ -649,12 +648,11 @@ def attached_pane(self) -> Pane | None:
649648
650649
Deprecated in favor of :meth:`.active_pane`.
651650
"""
652-
warnings.warn(
653-
"Session.attached_pane() is deprecated in favor of Session.active_pane()",
654-
category=DeprecationWarning,
655-
stacklevel=2,
651+
raise exc.DeprecatedError(
652+
deprecated="Session.attached_pane",
653+
replacement="Session.active_pane",
654+
version="0.31.0",
656655
)
657-
return self.active_window.active_pane
658656

659657
@property
660658
def attached_window(self) -> Window:
@@ -666,13 +664,11 @@ def attached_window(self) -> Window:
666664
667665
Deprecated in favor of :meth:`.active_window`.
668666
"""
669-
warnings.warn(
670-
"Session.attached_window() is deprecated in favor of "
671-
"Session.active_window()",
672-
category=DeprecationWarning,
673-
stacklevel=2,
667+
raise exc.DeprecatedError(
668+
deprecated="Session.attached_window",
669+
replacement="Session.active_window",
670+
version="0.31.0",
674671
)
675-
return self.active_window
676672

677673
def attach_session(self) -> Session:
678674
"""Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``.
@@ -683,17 +679,11 @@ def attach_session(self) -> Session:
683679
684680
Deprecated in favor of :meth:`.attach()`.
685681
"""
686-
warnings.warn(
687-
"Session.attach_session() is deprecated in favor of Session.attach()",
688-
category=DeprecationWarning,
689-
stacklevel=2,
682+
raise exc.DeprecatedError(
683+
deprecated="Session.attach_session()",
684+
replacement="Session.attach()",
685+
version="0.30.0",
690686
)
691-
proc = self.cmd("attach-session")
692-
693-
if proc.stderr:
694-
raise exc.LibTmuxException(proc.stderr)
695-
696-
return self
697687

698688
def kill_session(self) -> None:
699689
"""Destroy session.
@@ -704,15 +694,11 @@ def kill_session(self) -> None:
704694
705695
Deprecated in favor of :meth:`.kill()`.
706696
"""
707-
warnings.warn(
708-
"Session.kill_session() is deprecated in favor of Session.kill()",
709-
category=DeprecationWarning,
710-
stacklevel=2,
697+
raise exc.DeprecatedError(
698+
deprecated="Session.kill_session()",
699+
replacement="Session.kill()",
700+
version="0.30.0",
711701
)
712-
proc = self.cmd("kill-session")
713-
714-
if proc.stderr:
715-
raise exc.LibTmuxException(proc.stderr)
716702

717703
def get(self, key: str, default: t.Any | None = None) -> t.Any:
718704
"""Return key-based lookup. Deprecated by attributes.
@@ -723,12 +709,11 @@ def get(self, key: str, default: t.Any | None = None) -> t.Any:
723709
accessed via ``session.session_name``.
724710
725711
"""
726-
warnings.warn(
727-
"Session.get() is deprecated",
728-
category=DeprecationWarning,
729-
stacklevel=2,
712+
raise exc.DeprecatedError(
713+
deprecated="Session.get()",
714+
replacement="direct attribute access (e.g., session.session_name)",
715+
version="0.16.0",
730716
)
731-
return getattr(self, key, default)
732717

733718
def __getitem__(self, key: str) -> t.Any:
734719
"""Return item lookup by key. Deprecated in favor of attributes.
@@ -739,12 +724,11 @@ def __getitem__(self, key: str) -> t.Any:
739724
accessed via ``session.session_name``.
740725
741726
"""
742-
warnings.warn(
743-
f"Item lookups, e.g. session['{key}'] is deprecated",
744-
category=DeprecationWarning,
745-
stacklevel=2,
727+
raise exc.DeprecatedError(
728+
deprecated="Session[key] lookup",
729+
replacement="direct attribute access (e.g., session.session_name)",
730+
version="0.16.0",
746731
)
747-
return getattr(self, key)
748732

749733
def get_by_id(self, session_id: str) -> Window | None:
750734
"""Return window by id. Deprecated in favor of :meth:`.windows.get()`.
@@ -754,12 +738,11 @@ def get_by_id(self, session_id: str) -> Window | None:
754738
Deprecated by :meth:`.windows.get()`.
755739
756740
"""
757-
warnings.warn(
758-
"Session.get_by_id() is deprecated",
759-
category=DeprecationWarning,
760-
stacklevel=2,
741+
raise exc.DeprecatedError(
742+
deprecated="Session.get_by_id()",
743+
replacement="Session.windows.get(window_id=..., default=None)",
744+
version="0.16.0",
761745
)
762-
return self.windows.get(window_id=session_id, default=None)
763746

764747
def where(self, kwargs: dict[str, t.Any]) -> list[Window]:
765748
"""Filter through windows, return list of :class:`Window`.
@@ -769,15 +752,11 @@ def where(self, kwargs: dict[str, t.Any]) -> list[Window]:
769752
Deprecated by :meth:`.windows.filter()`.
770753
771754
"""
772-
warnings.warn(
773-
"Session.where() is deprecated",
774-
category=DeprecationWarning,
775-
stacklevel=2,
755+
raise exc.DeprecatedError(
756+
deprecated="Session.where()",
757+
replacement="Session.windows.filter()",
758+
version="0.16.0",
776759
)
777-
try:
778-
return self.windows.filter(**kwargs)
779-
except IndexError:
780-
return []
781760

782761
def find_where(self, kwargs: dict[str, t.Any]) -> Window | None:
783762
"""Filter through windows, return first :class:`Window`.
@@ -787,12 +766,11 @@ def find_where(self, kwargs: dict[str, t.Any]) -> Window | None:
787766
Slated to be removed in favor of :meth:`.windows.get()`.
788767
789768
"""
790-
warnings.warn(
791-
"Session.find_where() is deprecated",
792-
category=DeprecationWarning,
793-
stacklevel=2,
769+
raise exc.DeprecatedError(
770+
deprecated="Session.find_where()",
771+
replacement="Session.windows.get(default=None, **kwargs)",
772+
version="0.16.0",
794773
)
795-
return self.windows.get(default=None, **kwargs)
796774

797775
def _list_windows(self) -> list[WindowDict]:
798776
"""Return list of windows (deprecated in favor of :attr:`.windows`).
@@ -802,12 +780,11 @@ def _list_windows(self) -> list[WindowDict]:
802780
Slated to be removed in favor of :attr:`.windows`.
803781
804782
"""
805-
warnings.warn(
806-
"Session._list_windows() is deprecated",
807-
category=DeprecationWarning,
808-
stacklevel=2,
783+
raise exc.DeprecatedError(
784+
deprecated="Session._list_windows()",
785+
replacement="Session.windows property",
786+
version="0.16.0",
809787
)
810-
return [w.__dict__ for w in self.windows]
811788

812789
@property
813790
def _windows(self) -> list[WindowDict]:
@@ -818,12 +795,11 @@ def _windows(self) -> list[WindowDict]:
818795
Slated to be removed in favor of :attr:`.windows`.
819796
820797
"""
821-
warnings.warn(
822-
"Session._windows is deprecated",
823-
category=DeprecationWarning,
824-
stacklevel=2,
798+
raise exc.DeprecatedError(
799+
deprecated="Session._windows",
800+
replacement="Session.windows property",
801+
version="0.16.0",
825802
)
826-
return self._list_windows()
827803

828804
def list_windows(self) -> list[Window]:
829805
"""Return a list of :class:`Window` from the ``tmux(1)`` session.
@@ -833,12 +809,11 @@ def list_windows(self) -> list[Window]:
833809
Slated to be removed in favor of :attr:`.windows`.
834810
835811
"""
836-
warnings.warn(
837-
"Session.list_windows() is deprecated",
838-
category=DeprecationWarning,
839-
stacklevel=2,
812+
raise exc.DeprecatedError(
813+
deprecated="Session.list_windows()",
814+
replacement="Session.windows property",
815+
version="0.16.0",
840816
)
841-
return self.windows
842817

843818
@property
844819
def children(self) -> QueryList[Window]:
@@ -849,9 +824,8 @@ def children(self) -> QueryList[Window]:
849824
Slated to be removed in favor of :attr:`.windows`.
850825
851826
"""
852-
warnings.warn(
853-
"Session.children is deprecated",
854-
category=DeprecationWarning,
855-
stacklevel=2,
827+
raise exc.DeprecatedError(
828+
deprecated="Session.children",
829+
replacement="Session.windows property",
830+
version="0.16.0",
856831
)
857-
return self.windows

0 commit comments

Comments
 (0)