Skip to content

Commit 2cbcc00

Browse files
committed
refactor!(typings): MonkeyType typings (automated + black + isort)
1 parent 102f456 commit 2cbcc00

13 files changed

+201
-150
lines changed

libtmux/pane.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"""
88
import logging
99
import typing as t
10+
from typing import Dict
11+
12+
from libtmux.common import tmux_cmd
1013

1114
from . import exc
1215
from .common import TmuxMappingObject, TmuxRelationalObject
@@ -58,7 +61,7 @@ class Pane(TmuxMappingObject, TmuxRelationalObject):
5861
server: "Server"
5962
""":class:`libtmux.Server` pane is linked to"""
6063

61-
def __init__(self, window: "Window", **kwargs):
64+
def __init__(self, window: "Window", **kwargs) -> None:
6265
self.window = window
6366
self.session = self.window.session
6467
self.server = self.session.server
@@ -68,7 +71,7 @@ def __init__(self, window: "Window", **kwargs):
6871
self.server._update_panes()
6972

7073
@property
71-
def _info(self):
74+
def _info(self) -> Dict[str, str]:
7275

7376
attrs = {"pane_id": self._pane_id}
7477

@@ -86,7 +89,7 @@ def by(val) -> bool:
8689
target_panes = list(filter(by, self.server._panes))
8790
return target_panes[0]
8891

89-
def cmd(self, cmd, *args, **kwargs):
92+
def cmd(self, cmd: str, *args, **kwargs) -> tmux_cmd:
9093
"""Return :meth:`Server.cmd` defaulting to ``target_pane`` as target.
9194
9295
Send command to tmux with :attr:`pane_id` as ``target-pane``.
@@ -103,7 +106,13 @@ def cmd(self, cmd, *args, **kwargs):
103106

104107
return self.server.cmd(cmd, *args, **kwargs)
105108

106-
def send_keys(self, cmd, enter=True, suppress_history=True, literal=False):
109+
def send_keys(
110+
self,
111+
cmd: str,
112+
enter: bool = True,
113+
suppress_history: bool = True,
114+
literal: bool = False,
115+
) -> None:
107116
"""
108117
``$ tmux send-keys`` to the pane.
109118
@@ -159,7 +168,7 @@ def clear(self):
159168
"""Clear pane."""
160169
self.send_keys("reset")
161170

162-
def reset(self):
171+
def reset(self) -> None:
163172
"""Reset and clear pane history."""
164173

165174
self.cmd("send-keys", r"-R \; clear-history")
@@ -193,7 +202,7 @@ def split_window(
193202
percent=percent,
194203
)
195204

196-
def set_width(self, width):
205+
def set_width(self, width: int) -> None:
197206
"""
198207
Set width of pane.
199208
@@ -204,7 +213,7 @@ def set_width(self, width):
204213
"""
205214
self.resize_pane(width=width)
206215

207-
def set_height(self, height):
216+
def set_height(self, height: int) -> None:
208217
"""
209218
Set height of pane.
210219
@@ -215,7 +224,7 @@ def set_height(self, height):
215224
"""
216225
self.resize_pane(height=height)
217226

218-
def resize_pane(self, *args, **kwargs):
227+
def resize_pane(self, *args, **kwargs) -> "Pane":
219228
"""
220229
``$ tmux resize-pane`` of pane and return ``self``.
221230
@@ -253,7 +262,7 @@ def resize_pane(self, *args, **kwargs):
253262
self.server._update_panes()
254263
return self
255264

256-
def enter(self):
265+
def enter(self) -> None:
257266
"""
258267
Send carriage return to pane.
259268

libtmux/server.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import os
99
import typing as t
1010

11+
from libtmux.common import tmux_cmd
12+
from libtmux.session import Session
13+
1114
from . import exc, formats
1215
from .common import (
1316
EnvironmentMixin,
@@ -17,9 +20,7 @@
1720
WindowDict,
1821
has_gte_version,
1922
session_check_name,
20-
tmux_cmd,
2123
)
22-
from .session import Session
2324

2425
logger = logging.getLogger(__name__)
2526

@@ -75,12 +76,12 @@ class Server(TmuxRelationalObject, EnvironmentMixin):
7576

7677
def __init__(
7778
self,
78-
socket_name=None,
79-
socket_path=None,
80-
config_file=None,
81-
colors=None,
79+
socket_name: t.Optional[str] = None,
80+
socket_path: t.Optional[str] = None,
81+
config_file: t.Optional[str] = None,
82+
colors: t.Optional[int] = None,
8283
**kwargs,
83-
):
84+
) -> None:
8485
EnvironmentMixin.__init__(self, "-g")
8586
self._windows = []
8687
self._panes = []
@@ -97,7 +98,7 @@ def __init__(
9798
if colors:
9899
self.colors = colors
99100

100-
def cmd(self, *args, **kwargs):
101+
def cmd(self, *args, **kwargs) -> tmux_cmd:
101102
"""
102103
Execute tmux command and return output.
103104
@@ -382,7 +383,7 @@ def has_session(self, target_session: str, exact: bool = True) -> bool:
382383

383384
return False
384385

385-
def kill_server(self):
386+
def kill_server(self) -> None:
386387
"""``$ tmux kill-server``."""
387388
self.cmd("kill-server")
388389

@@ -413,7 +414,7 @@ def kill_session(self, target_session=None) -> "Server":
413414

414415
return self
415416

416-
def switch_client(self, target_session):
417+
def switch_client(self, target_session: str):
417418
"""
418419
``$ tmux switch-client``.
419420
@@ -433,7 +434,7 @@ def switch_client(self, target_session):
433434
if proc.stderr:
434435
raise exc.LibTmuxException(proc.stderr)
435436

436-
def attach_session(self, target_session=None):
437+
def attach_session(self, target_session: t.Optional[str] = None):
437438
"""``$ tmux attach-session`` aka alias: ``$ tmux attach``.
438439
439440
Parameters
@@ -458,12 +459,12 @@ def attach_session(self, target_session=None):
458459

459460
def new_session(
460461
self,
461-
session_name=None,
462-
kill_session=False,
463-
attach=False,
464-
start_directory=None,
465-
window_name=None,
466-
window_command=None,
462+
session_name: t.Optional[str] = None,
463+
kill_session: bool = False,
464+
attach: bool = False,
465+
start_directory: None = None,
466+
window_name: None = None,
467+
window_command: t.Optional[str] = None,
467468
*args,
468469
**kwargs,
469470
) -> Session:

libtmux/session.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import logging
88
import os
99
import typing as t
10+
from typing import Dict, Optional, Union
11+
12+
from libtmux.common import tmux_cmd
13+
from libtmux.window import Window
1014

1115
from . import exc, formats
1216
from .common import (
@@ -18,7 +22,6 @@
1822
has_version,
1923
session_check_name,
2024
)
21-
from .window import Window
2225

2326
if t.TYPE_CHECKING:
2427
from .pane import Pane
@@ -58,7 +61,7 @@ class Session(TmuxMappingObject, TmuxRelationalObject, EnvironmentMixin):
5861
server: "Server"
5962
""":class:`libtmux.server.Server` session is linked to"""
6063

61-
def __init__(self, server: "Server", **kwargs):
64+
def __init__(self, server: "Server", **kwargs) -> None:
6265
EnvironmentMixin.__init__(self)
6366
self.server = server
6467

@@ -68,7 +71,7 @@ def __init__(self, server: "Server", **kwargs):
6871
self.server._update_windows()
6972

7073
@property
71-
def _info(self):
74+
def _info(self) -> Dict[str, str]:
7275

7376
attrs = {"session_id": str(self._session_id)}
7477

@@ -88,7 +91,7 @@ def by(val) -> bool:
8891
except IndexError as e:
8992
logger.error(e)
9093

91-
def cmd(self, *args, **kwargs):
94+
def cmd(self, *args, **kwargs) -> tmux_cmd:
9295
"""
9396
Return :meth:`server.cmd`.
9497
@@ -177,11 +180,11 @@ def rename_session(self, new_name: str) -> "Session":
177180

178181
def new_window(
179182
self,
180-
window_name=None,
181-
start_directory=None,
182-
attach=True,
183-
window_index="",
184-
window_shell=None,
183+
window_name: Optional[str] = None,
184+
start_directory: None = None,
185+
attach: bool = True,
186+
window_index: str = "",
187+
window_shell: None = None,
185188
) -> Window:
186189
"""
187190
Return :class:`Window` from ``$ tmux new-window``.
@@ -259,7 +262,7 @@ def new_window(
259262

260263
return window
261264

262-
def kill_window(self, target_window=None):
265+
def kill_window(self, target_window: Optional[str] = None) -> None:
263266
"""Close a tmux window, and all panes inside it, ``$ tmux kill-window``
264267
265268
Kill the current window or the window at ``target-window``. removing it
@@ -378,7 +381,9 @@ def attached_pane(self) -> t.Optional["Pane"]:
378381

379382
return self.attached_window.attached_pane
380383

381-
def set_option(self, option, value, _global=False):
384+
def set_option(
385+
self, option: str, value: Union[str, int], _global: bool = False
386+
) -> None:
382387
"""
383388
Set option ``$ tmux set-option <option> <value>``.
384389
@@ -421,7 +426,9 @@ def set_option(self, option, value, _global=False):
421426
if isinstance(proc.stderr, list) and len(proc.stderr):
422427
handle_option_error(proc.stderr[0])
423428

424-
def show_options(self, _global=False):
429+
def show_options(
430+
self, _global: Optional[bool] = False
431+
) -> Dict[str, Union[str, int]]:
425432
"""
426433
Return a dict of options for the window.
427434
@@ -460,7 +467,9 @@ def show_options(self, _global=False):
460467

461468
return session_options
462469

463-
def show_option(self, option, _global=False):
470+
def show_option(
471+
self, option: str, _global: bool = False
472+
) -> Optional[Union[str, int]]:
464473
"""Return a list of options for the window.
465474
466475
Parameters

libtmux/test.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import warnings
88
from typing import Callable, Optional
99

10+
from libtmux.server import Server
11+
1012
from .exc import WaitTimeout
1113

1214
logger = logging.getLogger(__name__)
@@ -17,13 +19,15 @@
1719

1820

1921
class RandomStrSequence:
20-
def __init__(self, characters: str = "abcdefghijklmnopqrstuvwxyz0123456789_"):
22+
def __init__(
23+
self, characters: str = "abcdefghijklmnopqrstuvwxyz0123456789_"
24+
) -> None:
2125
self.characters: str = characters
2226

2327
def __iter__(self):
2428
return self
2529

26-
def __next__(self):
30+
def __next__(self) -> str:
2731
return "".join(random.sample(self.characters, k=8))
2832

2933

@@ -119,7 +123,7 @@ def retry_until(
119123
return True
120124

121125

122-
def get_test_session_name(server, prefix=TEST_SESSION_PREFIX):
126+
def get_test_session_name(server: Server, prefix: str = TEST_SESSION_PREFIX) -> str:
123127
"""
124128
Faker to create a session name that doesn't exist.
125129

0 commit comments

Comments
 (0)