Skip to content

Commit c0fe8bb

Browse files
committed
deprecations(refactor): Replace DeprecationWarning with DeprecatedError (#611)
why: Legacy APIs deprecated in 0.16-0.33 should now raise hard errors instead of soft warnings to encourage migration to new APIs. what: - Add DeprecatedError exception class in exc.py - Server: kill_server, get_by_id, where, find_where, list_sessions, etc. - Session: attach_session, kill_session, attached_window/pane, etc. - Window: split_window, select_window, kill_window, attached_pane, etc. - Pane: resize_pane, select_pane, split_window, dict access - Keep 0.50 deprecations (g param, window options) as soft warnings
1 parent d6a8838 commit c0fe8bb

File tree

6 files changed

+262
-335
lines changed

6 files changed

+262
-335
lines changed

src/libtmux/exc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ class LibTmuxException(Exception):
1919
"""Base Exception for libtmux Errors."""
2020

2121

22+
class DeprecatedError(LibTmuxException):
23+
"""Raised when a deprecated function, method, or parameter is used.
24+
25+
This exception provides clear guidance on what to use instead.
26+
27+
Parameters
28+
----------
29+
deprecated : str
30+
The name of the deprecated API (e.g., "Pane.resize_pane()")
31+
replacement : str
32+
The recommended replacement API to use instead
33+
version : str
34+
The version when the API was deprecated (e.g., "0.28.0")
35+
"""
36+
37+
def __init__(
38+
self,
39+
*,
40+
deprecated: str,
41+
replacement: str,
42+
version: str,
43+
) -> None:
44+
msg = (
45+
f"{deprecated} was deprecated in {version} and has been removed. "
46+
f"Use {replacement} instead."
47+
)
48+
super().__init__(msg)
49+
50+
2251
class TmuxSessionExists(LibTmuxException):
2352
"""Session does not exist in the server."""
2453

src/libtmux/options.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,9 @@ def _show_options_raw(
797797
798798
Parameters
799799
----------
800-
g : str, optional
801-
Pass ``-g`` flag for global variable, default False.
800+
g : bool, optional
801+
.. deprecated:: 0.50.0
802+
Use ``global_`` instead. Raises :exc:`~libtmux.exc.DeprecatedError`.
802803
803804
Examples
804805
--------
@@ -834,8 +835,9 @@ def _show_options_raw(
834835
category=DeprecationWarning,
835836
stacklevel=2,
836837
)
837-
flags += ("-g",)
838-
elif global_:
838+
global_ = g
839+
840+
if global_:
839841
flags += ("-g",)
840842

841843
if scope is not None and not isinstance(scope, _DefaultOptionScope):
@@ -864,8 +866,9 @@ def _show_options_dict(
864866
865867
Parameters
866868
----------
867-
g : str, optional
868-
Pass ``-g`` flag for global variable, default False.
869+
g : bool, optional
870+
.. deprecated:: 0.50.0
871+
Use ``global_`` instead. Raises :exc:`~libtmux.exc.DeprecatedError`.
869872
870873
Examples
871874
--------
@@ -913,8 +916,9 @@ def _show_options(
913916
914917
Parameters
915918
----------
916-
g : str, optional
917-
Pass ``-g`` flag for global variable, default False.
919+
g : bool, optional
920+
.. deprecated:: 0.50.0
921+
Use ``global_`` instead. Raises :exc:`~libtmux.exc.DeprecatedError`.
918922
919923
Examples
920924
--------
@@ -1013,7 +1017,8 @@ def _show_option_raw(
10131017
----------
10141018
option : str
10151019
g : bool, optional
1016-
Pass ``-g`` flag, global. Default False.
1020+
.. deprecated:: 0.50.0
1021+
Use ``global_`` instead. Raises :exc:`~libtmux.exc.DeprecatedError`.
10171022
10181023
Raises
10191024
------
@@ -1063,8 +1068,9 @@ def _show_option_raw(
10631068
category=DeprecationWarning,
10641069
stacklevel=2,
10651070
)
1066-
flags += ("-g",)
1067-
elif global_:
1071+
global_ = g
1072+
1073+
if global_:
10681074
flags += ("-g",)
10691075

10701076
if scope is not None and not isinstance(scope, _DefaultOptionScope):
@@ -1104,7 +1110,8 @@ def _show_option(
11041110
----------
11051111
option : str
11061112
g : bool, optional
1107-
Pass ``-g`` flag, global. Default False.
1113+
.. deprecated:: 0.50.0
1114+
Use ``global_`` instead. Raises :exc:`~libtmux.exc.DeprecatedError`.
11081115
11091116
Raises
11101117
------
@@ -1206,7 +1213,8 @@ def show_option(
12061213
----------
12071214
option : str
12081215
g : bool, optional
1209-
Pass ``-g`` flag, global. Default False.
1216+
.. deprecated:: 0.50.0
1217+
Use ``global_`` instead. Raises :exc:`~libtmux.exc.DeprecatedError`.
12101218
12111219
Raises
12121220
------

src/libtmux/pane.py

Lines changed: 24 additions & 52 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 import exc
1716
from libtmux.common import tmux_cmd
@@ -554,16 +553,11 @@ def select_pane(self) -> Pane:
554553
555554
Deprecated in favor of :meth:`.select()`.
556555
"""
557-
warnings.warn(
558-
"Pane.select_pane() is deprecated in favor of Pane.select()",
559-
category=DeprecationWarning,
560-
stacklevel=2,
556+
raise exc.DeprecatedError(
557+
deprecated="Pane.select_pane()",
558+
replacement="Pane.select()",
559+
version="0.30.0",
561560
)
562-
assert isinstance(self.pane_id, str)
563-
pane = self.window.select_pane(self.pane_id)
564-
if pane is None:
565-
raise exc.PaneNotFound(pane_id=self.pane_id)
566-
return pane
567561

568562
def split(
569563
self,
@@ -731,7 +725,7 @@ def set_width(self, width: int) -> Pane:
731725
:class:`Pane`
732726
Self, for method chaining.
733727
"""
734-
self.resize_pane(width=width)
728+
self.resize(width=width)
735729
return self
736730

737731
def set_height(self, height: int) -> Pane:
@@ -747,7 +741,7 @@ def set_height(self, height: int) -> Pane:
747741
:class:`Pane`
748742
Self, for method chaining.
749743
"""
750-
self.resize_pane(height=height)
744+
self.resize(height=height)
751745
return self
752746

753747
def enter(self) -> Pane:
@@ -915,54 +909,41 @@ def split_window(
915909
916910
Deprecated in favor of :meth:`.split`.
917911
"""
918-
warnings.warn(
919-
"Pane.split_window() is deprecated in favor of Pane.split()",
920-
category=DeprecationWarning,
921-
stacklevel=2,
922-
)
923-
if size is None and percent is not None:
924-
size = f"{str(percent).rstrip('%')}%"
925-
return self.split(
926-
target=target,
927-
attach=attach,
928-
start_directory=start_directory,
929-
direction=PaneDirection.Below if vertical else PaneDirection.Right,
930-
shell=shell,
931-
size=size,
932-
environment=environment,
912+
raise exc.DeprecatedError(
913+
deprecated="Pane.split_window()",
914+
replacement="Pane.split()",
915+
version="0.33.0",
933916
)
934917

935918
def get(self, key: str, default: t.Any | None = None) -> t.Any:
936919
"""Return key-based lookup. Deprecated by attributes.
937920
938-
.. deprecated:: 0.16
921+
.. deprecated:: 0.17
939922
940923
Deprecated by attribute lookup, e.g. ``pane['window_name']`` is now
941924
accessed via ``pane.window_name``.
942925
943926
"""
944-
warnings.warn(
945-
"Pane.get() is deprecated",
946-
category=DeprecationWarning,
947-
stacklevel=2,
927+
raise exc.DeprecatedError(
928+
deprecated="Pane.get()",
929+
replacement="direct attribute access (e.g., pane.pane_id)",
930+
version="0.17.0",
948931
)
949-
return getattr(self, key, default)
950932

951933
def __getitem__(self, key: str) -> t.Any:
952934
"""Return item lookup by key. Deprecated in favor of attributes.
953935
954-
.. deprecated:: 0.16
936+
.. deprecated:: 0.17
955937
956938
Deprecated in favor of attributes. e.g. ``pane['window_name']`` is now
957939
accessed via ``pane.window_name``.
958940
959941
"""
960-
warnings.warn(
961-
f"Item lookups, e.g. pane['{key}'] is deprecated",
962-
category=DeprecationWarning,
963-
stacklevel=2,
942+
raise exc.DeprecatedError(
943+
deprecated="Pane[key] lookup",
944+
replacement="direct attribute access (e.g., pane.pane_id)",
945+
version="0.17.0",
964946
)
965-
return getattr(self, key)
966947

967948
def resize_pane(
968949
self,
@@ -985,17 +966,8 @@ def resize_pane(
985966
986967
Deprecated by :meth:`Pane.resize`.
987968
"""
988-
warnings.warn(
989-
"Deprecated: Use Pane.resize() instead of Pane.resize_pane()",
990-
category=DeprecationWarning,
991-
stacklevel=2,
992-
)
993-
return self.resize(
994-
adjustment_direction=adjustment_direction,
995-
adjustment=adjustment,
996-
height=height,
997-
width=width,
998-
zoom=zoom,
999-
mouse=mouse,
1000-
trim_below=trim_below,
969+
raise exc.DeprecatedError(
970+
deprecated="Pane.resize_pane()",
971+
replacement="Pane.resize()",
972+
version="0.28.0",
1001973
)

0 commit comments

Comments
 (0)