Skip to content

Adding unobtrusive type hinting #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libtmux/_compat.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# flake8: NOQA
import sys
import typing as t
from collections.abc import MutableMapping

console_encoding = sys.__stdout__.encoding


def console_to_str(s):
def console_to_str(s: bytes) -> str:
"""From pypa/pip project, pip.backwardwardcompat. License MIT."""
try:
return s.decode(console_encoding, "ignore")
except UnicodeDecodeError:
return s.decode("utf_8", "ignore")


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


def str_from_console(s):
def str_from_console(s: t.Union[str, bytes]) -> str:
try:
return str(s)
except UnicodeDecodeError:
Expand Down
37 changes: 22 additions & 15 deletions libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import subprocess
import sys
import typing as t
from distutils.version import LooseVersion

from . import exc
Expand Down Expand Up @@ -374,10 +375,16 @@ def get_by_id(self, id):


def which(
exe=None,
default_paths=["/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin"],
append_env_path=True,
):
exe: str,
default_paths: t.List[str] = [
"/bin",
"/sbin",
"/usr/bin",
"/usr/sbin",
"/usr/local/bin",
],
append_env_path: bool = True,
) -> t.Optional[str]:
"""
Return path of bin. Python clone of /usr/bin/which.

Expand All @@ -401,7 +408,7 @@ def which(
from salt.util - https://www.github.com/saltstack/salt - license apache
"""

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

Expand Down Expand Up @@ -434,7 +441,7 @@ def _is_executable_file_or_link(exe):
return None


def get_version():
def get_version() -> LooseVersion:
"""
Return tmux version.

Expand Down Expand Up @@ -471,7 +478,7 @@ def get_version():
return LooseVersion(version)


def has_version(version):
def has_version(version: str) -> bool:
"""
Return affirmative if tmux version installed.

Expand All @@ -488,7 +495,7 @@ def has_version(version):
return get_version() == LooseVersion(version)


def has_gt_version(min_version):
def has_gt_version(min_version: str) -> bool:
"""
Return affirmative if tmux version greater than minimum.

Expand All @@ -505,7 +512,7 @@ def has_gt_version(min_version):
return get_version() > LooseVersion(min_version)


def has_gte_version(min_version):
def has_gte_version(min_version: str) -> bool:
"""
Return True if tmux version greater or equal to minimum.

Expand All @@ -522,7 +529,7 @@ def has_gte_version(min_version):
return get_version() >= LooseVersion(min_version)


def has_lte_version(max_version):
def has_lte_version(max_version: str) -> bool:
"""
Return True if tmux version less or equal to minimum.

Expand All @@ -539,7 +546,7 @@ def has_lte_version(max_version):
return get_version() <= LooseVersion(max_version)


def has_lt_version(max_version):
def has_lt_version(max_version: str) -> bool:
"""
Return True if tmux version less than minimum.

Expand All @@ -556,7 +563,7 @@ def has_lt_version(max_version):
return get_version() < LooseVersion(max_version)


def has_minimum_version(raises=True):
def has_minimum_version(raises: bool = True) -> bool:
"""
Return if tmux meets version requirement. Version >1.8 or above.

Expand Down Expand Up @@ -598,7 +605,7 @@ def has_minimum_version(raises=True):
return True


def session_check_name(session_name):
def session_check_name(session_name: str):
"""
Raises exception session name invalid, modeled after tmux function.

Expand Down Expand Up @@ -627,7 +634,7 @@ def session_check_name(session_name):
)


def handle_option_error(error):
def handle_option_error(error: str):
"""Raises exception if error in option command found.

In tmux 3.0, show-option and show-window-otion return invalid option instead of
Expand Down Expand Up @@ -664,7 +671,7 @@ def handle_option_error(error):
raise exc.OptionError(error) # Raise generic option error


def get_libtmux_version():
def get_libtmux_version() -> LooseVersion:
"""Return libtmux version is a PEP386 compliant format.

Returns
Expand Down