Skip to content

Commit 25d68f3

Browse files
committed
Adding unobtrusive type hinting
1 parent 071f1dc commit 25d68f3

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

libtmux/_compat.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# flake8: NOQA
22
import sys
33
from collections.abc import MutableMapping
4+
from typing import Union
45

56
console_encoding = sys.__stdout__.encoding
67

78

8-
def console_to_str(s):
9+
def console_to_str(s: bytes) -> str:
910
"""From pypa/pip project, pip.backwardwardcompat. License MIT."""
1011
try:
1112
return s.decode(console_encoding, "ignore")
1213
except UnicodeDecodeError:
1314
return s.decode("utf_8", "ignore")
1415

1516

17+
# TODO Consider removing, reraise does not seem to be called anywhere
1618
def reraise(tp, value, tb=None):
1719
if value.__traceback__ is not tb:
1820
raise (value.with_traceback(tb))
1921
raise value
2022

2123

22-
def str_from_console(s):
24+
def str_from_console(s: Union[str, bytes]) -> str:
2325
try:
2426
return str(s)
2527
except UnicodeDecodeError:

libtmux/common.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import subprocess
1212
import sys
1313
from distutils.version import LooseVersion
14+
from typing import Optional
1415

1516
from . import exc
1617
from ._compat import MutableMapping, console_to_str, str_from_console
@@ -374,10 +375,16 @@ def get_by_id(self, id):
374375

375376

376377
def which(
377-
exe=None,
378-
default_paths=["/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin"],
379-
append_env_path=True,
380-
):
378+
exe: str,
379+
default_paths: list[str] = [
380+
"/bin",
381+
"/sbin",
382+
"/usr/bin",
383+
"/usr/sbin",
384+
"/usr/local/bin",
385+
],
386+
append_env_path: bool = True,
387+
) -> Optional[str]:
381388
"""
382389
Return path of bin. Python clone of /usr/bin/which.
383390
@@ -401,7 +408,7 @@ def which(
401408
from salt.util - https://www.github.com/saltstack/salt - license apache
402409
"""
403410

404-
def _is_executable_file_or_link(exe):
411+
def _is_executable_file_or_link(exe: str) -> bool:
405412
# check for os.X_OK doesn't suffice because directory may executable
406413
return os.access(exe, os.X_OK) and (os.path.isfile(exe) or os.path.islink(exe))
407414

@@ -434,7 +441,7 @@ def _is_executable_file_or_link(exe):
434441
return None
435442

436443

437-
def get_version():
444+
def get_version() -> LooseVersion:
438445
"""
439446
Return tmux version.
440447
@@ -471,7 +478,7 @@ def get_version():
471478
return LooseVersion(version)
472479

473480

474-
def has_version(version):
481+
def has_version(version: str) -> bool:
475482
"""
476483
Return affirmative if tmux version installed.
477484
@@ -488,7 +495,7 @@ def has_version(version):
488495
return get_version() == LooseVersion(version)
489496

490497

491-
def has_gt_version(min_version):
498+
def has_gt_version(min_version: str) -> bool:
492499
"""
493500
Return affirmative if tmux version greater than minimum.
494501
@@ -505,7 +512,7 @@ def has_gt_version(min_version):
505512
return get_version() > LooseVersion(min_version)
506513

507514

508-
def has_gte_version(min_version):
515+
def has_gte_version(min_version: str) -> bool:
509516
"""
510517
Return True if tmux version greater or equal to minimum.
511518
@@ -522,7 +529,7 @@ def has_gte_version(min_version):
522529
return get_version() >= LooseVersion(min_version)
523530

524531

525-
def has_lte_version(max_version):
532+
def has_lte_version(max_version: str) -> bool:
526533
"""
527534
Return True if tmux version less or equal to minimum.
528535
@@ -539,7 +546,7 @@ def has_lte_version(max_version):
539546
return get_version() <= LooseVersion(max_version)
540547

541548

542-
def has_lt_version(max_version):
549+
def has_lt_version(max_version: str) -> bool:
543550
"""
544551
Return True if tmux version less than minimum.
545552
@@ -556,7 +563,7 @@ def has_lt_version(max_version):
556563
return get_version() < LooseVersion(max_version)
557564

558565

559-
def has_minimum_version(raises=True):
566+
def has_minimum_version(raises: bool = True) -> bool:
560567
"""
561568
Return if tmux meets version requirement. Version >1.8 or above.
562569
@@ -598,7 +605,7 @@ def has_minimum_version(raises=True):
598605
return True
599606

600607

601-
def session_check_name(session_name):
608+
def session_check_name(session_name: str):
602609
"""
603610
Raises exception session name invalid, modeled after tmux function.
604611
@@ -627,7 +634,7 @@ def session_check_name(session_name):
627634
)
628635

629636

630-
def handle_option_error(error):
637+
def handle_option_error(error: str):
631638
"""Raises exception if error in option command found.
632639
633640
In tmux 3.0, show-option and show-window-otion return invalid option instead of
@@ -664,7 +671,7 @@ def handle_option_error(error):
664671
raise exc.OptionError(error) # Raise generic option error
665672

666673

667-
def get_libtmux_version():
674+
def get_libtmux_version() -> LooseVersion:
668675
"""Return libtmux version is a PEP386 compliant format.
669676
670677
Returns

0 commit comments

Comments
 (0)