Skip to content

Commit 18acf8f

Browse files
authored
Core relational typings: Server, Session, Window, Pane (#385)
These will make it much easier to reason about dependent relations Not as thorough as #383 will be
2 parents c9283c9 + e5ef48e commit 18acf8f

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

CHANGES

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ $ pip install --user --upgrade --pre libtmux
2929

3030
This created issues with running poetry while inside the virtualenv.
3131

32+
- Typings
33+
- Core level relations, e.g. `Pane.window`, `Pane.session`, `Pane.server`, `Window.server` {issue}`385`
34+
3235
### Documentation
3336

3437
- Renewed logo

libtmux/pane.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
77
"""
88
import logging
9+
import typing as t
910

1011
from . import exc
1112
from .common import TmuxMappingObject, TmuxRelationalObject
1213

14+
if t.TYPE_CHECKING:
15+
from .server import Server
16+
from .session import Session
17+
from .window import Window
18+
19+
1320
logger = logging.getLogger(__name__)
1421

1522

@@ -20,7 +27,7 @@ class Pane(TmuxMappingObject, TmuxRelationalObject):
2027
``Pane`` instances can send commands directly to a pane, or traverse
2128
between linked tmux objects.
2229
23-
Parameters
30+
Attributes
2431
----------
2532
window : :class:`Window`
2633
@@ -42,13 +49,16 @@ class Pane(TmuxMappingObject, TmuxRelationalObject):
4249
Accessed April 1st, 2018.
4350
"""
4451

45-
#: namespace used :class:`~libtmux.common.TmuxMappingObject`
4652
formatter_prefix = "pane_"
47-
48-
def __init__(self, window=None, **kwargs):
49-
if not window:
50-
raise ValueError("Pane must have ``Window`` object")
51-
53+
"""Namespace used for :class:`~libtmux.common.TmuxMappingObject`"""
54+
window: "Window"
55+
""":class:`libtmux.Window` pane is linked to"""
56+
session: "Session"
57+
""":class:`libtmux.Session` pane is linked to"""
58+
server: "Server"
59+
""":class:`libtmux.Server` pane is linked to"""
60+
61+
def __init__(self, window: "Window", **kwargs):
5262
self.window = window
5363
self.session = self.window.session
5464
self.server = self.session.server
@@ -275,7 +285,10 @@ def select_pane(self) -> "Pane":
275285
-------
276286
:class:`pane`
277287
"""
278-
return self.window.select_pane(self.get("pane_id"))
288+
pane = self.window.select_pane(self._pane_id)
289+
if pane is None:
290+
raise exc.LibTmuxException(f"Pane not found: {self}")
291+
return pane
279292

280293
def __repr__(self) -> str:
281294
return "{}({} {})".format(

libtmux/server.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ class Server(TmuxRelationalObject, EnvironmentMixin):
6060
Accessed April 1st, 2018.
6161
"""
6262

63-
#: ``[-L socket-name]``
6463
socket_name = None
65-
#: ``[-S socket-path]``
64+
"""Passthrough to ``[-L socket-name]``"""
6665
socket_path = None
67-
#: ``[-f file]``
66+
"""Passthrough to ``[-S socket-path]``"""
6867
config_file = None
69-
#: ``-2`` or ``-8``
68+
"""Passthrough to ``[-f file]``"""
7069
colors = None
71-
#: unique child ID used by :class:`~libtmux.common.TmuxRelationalObject`
70+
"""``-2`` or ``-8``"""
7271
child_id_attribute = "session_id"
73-
#: namespace used :class:`~libtmux.common.TmuxMappingObject`
72+
"""Unique child ID used by :class:`~libtmux.common.TmuxRelationalObject`"""
7473
formatter_prefix = "server_"
74+
"""Namespace used for :class:`~libtmux.common.TmuxMappingObject`"""
7575

7676
def __init__(
7777
self,

libtmux/session.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
if t.TYPE_CHECKING:
2424
from .pane import Pane
25+
from .server import Server
2526

2627

2728
logger = logging.getLogger(__name__)
@@ -50,12 +51,14 @@ class Session(TmuxMappingObject, TmuxRelationalObject, EnvironmentMixin):
5051
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
5152
"""
5253

53-
#: unique child ID key for :class:`~libtmux.common.TmuxRelationalObject`
5454
child_id_attribute = "window_id"
55-
#: namespace used :class:`~libtmux.common.TmuxMappingObject`
55+
"""Unique child ID key for :class:`~libtmux.common.TmuxRelationalObject`"""
5656
formatter_prefix = "session_"
57+
"""Namespace used for :class:`~libtmux.common.TmuxMappingObject`"""
58+
server: "Server"
59+
""":class:`libtmux.server.Server` session is linked to"""
5760

58-
def __init__(self, server=None, **kwargs):
61+
def __init__(self, server: "Server", **kwargs):
5962
EnvironmentMixin.__init__(self)
6063
self.server = server
6164

libtmux/window.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
)
1919
from .pane import Pane
2020

21+
if t.TYPE_CHECKING:
22+
from .server import Server
23+
from .session import Session
24+
2125
logger = logging.getLogger(__name__)
2226

2327

@@ -41,16 +45,16 @@ class Window(TmuxMappingObject, TmuxRelationalObject):
4145
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
4246
"""
4347

44-
#: unique child ID key for :class:`~libtmux.common.TmuxRelationalObject`
4548
child_id_attribute = "pane_id"
46-
#: namespace used :class:`~libtmux.common.TmuxMappingObject`
49+
"""Unique child ID key for :class:`~libtmux.common.TmuxRelationalObject`"""
4750
formatter_prefix = "window_"
51+
"""Namespace used for :class:`~libtmux.common.TmuxMappingObject`"""
52+
server: "Server"
53+
""":class:`libtmux.Server` window is linked to"""
54+
session: "Session"
55+
""":class:`libtmux.Session` window is linked to"""
4856

49-
def __init__(self, session=None, **kwargs):
50-
51-
if not session:
52-
raise ValueError("Window requires a Session, session=Session")
53-
57+
def __init__(self, session: "Session", **kwargs):
5458
self.session = session
5559
self.server = self.session.server
5660

0 commit comments

Comments
 (0)