From 60472fcee3a19308ea01d485c7fc96e0d7919bc0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Oct 2021 14:00:29 +0530 Subject: [PATCH] Add more type annotations --- kittens/tui/handler.py | 8 ++++---- kittens/tui/images.py | 2 +- kittens/tui/loop.py | 14 ++++++-------- kittens/tui/operations.py | 8 ++++---- kitty/fonts/box_drawing.py | 10 +++++----- kitty/marks.py | 2 +- kitty/notify.py | 4 ++-- 7 files changed, 23 insertions(+), 25 deletions(-) diff --git a/kittens/tui/handler.py b/kittens/tui/handler.py index 0c2b7aad7..bd128704e 100644 --- a/kittens/tui/handler.py +++ b/kittens/tui/handler.py @@ -156,7 +156,7 @@ class Handler: def on_writing_finished(self) -> None: pass - def on_kitty_cmd_response(self, response: Dict) -> None: + def on_kitty_cmd_response(self, response: Dict[str, Any]) -> None: pass def on_clipboard_response(self, text: str, from_primary: bool = False) -> None: @@ -199,7 +199,7 @@ class HandleResult: type_of_input: Optional[str] = None no_ui: bool = False - def __init__(self, impl: Callable, type_of_input: Optional[str], no_ui: bool): + def __init__(self, impl: Callable[..., Any], type_of_input: Optional[str], no_ui: bool): self.impl = impl self.no_ui = no_ui self.type_of_input = type_of_input @@ -208,9 +208,9 @@ class HandleResult: return self.impl(args, data, target_window_id, boss) -def result_handler(type_of_input: Optional[str] = None, no_ui: bool = False) -> Callable[[Callable], HandleResult]: +def result_handler(type_of_input: Optional[str] = None, no_ui: bool = False) -> Callable[[Callable[..., Any]], HandleResult]: - def wrapper(impl: Callable) -> HandleResult: + def wrapper(impl: Callable[..., Any]) -> HandleResult: return HandleResult(impl, type_of_input, no_ui) return wrapper diff --git a/kittens/tui/images.py b/kittens/tui/images.py index 5d18f888c..ec794e368 100644 --- a/kittens/tui/images.py +++ b/kittens/tui/images.py @@ -132,7 +132,7 @@ class OutdatedImageMagick(ValueError): last_imagemagick_cmd: Sequence[str] = () -def run_imagemagick(path: str, cmd: Sequence[str], keep_stdout: bool = True) -> CompletedProcess: +def run_imagemagick(path: str, cmd: Sequence[str], keep_stdout: bool = True) -> 'CompletedProcess[bytes]': global last_imagemagick_cmd import subprocess last_imagemagick_cmd = cmd diff --git a/kittens/tui/loop.py b/kittens/tui/loop.py index 982c5431b..e44ec5cfc 100644 --- a/kittens/tui/loop.py +++ b/kittens/tui/loop.py @@ -193,19 +193,17 @@ class SignalManager: def __init__( self, loop: asyncio.AbstractEventLoop, - on_winch: Callable, - on_interrupt: Callable, - on_term: Callable + on_winch: Callable[[], None], + on_interrupt: Callable[[], None], + on_term: Callable[[], None], ) -> None: self.asycio_loop = loop self.on_winch, self.on_interrupt, self.on_term = on_winch, on_interrupt, on_term def __enter__(self) -> None: - tuple(map(lambda x: self.asycio_loop.add_signal_handler(*x), ( - (signal.SIGWINCH, self.on_winch), - (signal.SIGINT, self.on_interrupt), - (signal.SIGTERM, self.on_term) - ))) + self.asycio_loop.add_signal_handler(signal.SIGWINCH, self.on_winch) + self.asycio_loop.add_signal_handler(signal.SIGINT, self.on_interrupt) + self.asycio_loop.add_signal_handler(signal.SIGTERM, self.on_term) def __exit__(self, *a: Any) -> None: tuple(map(self.asycio_loop.remove_signal_handler, ( diff --git a/kittens/tui/operations.py b/kittens/tui/operations.py index 634d29f0c..8e752fc92 100644 --- a/kittens/tui/operations.py +++ b/kittens/tui/operations.py @@ -23,7 +23,7 @@ RESTORE_PRIVATE_MODE_VALUES = '\033[?r' SAVE_COLORS = '\033[#P' RESTORE_COLORS = '\033[#Q' F = TypeVar('F') -all_cmds: Dict[str, Callable] = {} +all_cmds: Dict[str, Callable[..., Any]] = {} class Mode(Enum): @@ -264,7 +264,7 @@ def serialize_gr_command(cmd: Dict[str, Union[int, str]], payload: Optional[byte @cmd -def gr_command(cmd: Union[Dict, 'GraphicsCommandType'], payload: Optional[bytes] = None) -> str: +def gr_command(cmd: Union[Dict[str, Union[int, str]], 'GraphicsCommandType'], payload: Optional[bytes] = None) -> str: if isinstance(cmd, dict): raw = serialize_gr_command(cmd, payload) else: @@ -428,7 +428,7 @@ def request_from_clipboard(use_primary: bool = False) -> str: # Boilerplate to make operations available via Handler.cmd {{{ -def writer(handler: HandlerType, func: Callable) -> Callable: +def writer(handler: HandlerType, func: Callable[..., Union[bytes, str]]) -> Callable[..., None]: @wraps(func) def f(*a: Any, **kw: Any) -> None: handler.write(func(*a, **kw)) @@ -442,7 +442,7 @@ def commander(handler: HandlerType) -> CMD: return ans -def func_sig(func: Callable) -> Generator[str, None, None]: +def func_sig(func: Callable[..., Any]) -> Generator[str, None, None]: import inspect import re s = inspect.signature(func) diff --git a/kitty/fonts/box_drawing.py b/kitty/fonts/box_drawing.py index 5ae81e7af..704e03c8b 100644 --- a/kitty/fonts/box_drawing.py +++ b/kitty/fonts/box_drawing.py @@ -11,7 +11,7 @@ from functools import partial as p, wraps from itertools import repeat from typing import ( Any, Callable, Dict, Iterable, Iterator, List, MutableSequence, Optional, - Sequence, Tuple, cast + Sequence, Tuple ) scale = (0.001, 1., 1.5, 2.) @@ -168,11 +168,11 @@ class SSByteArray(bytearray): supersample_factor = 1 -def supersampled(supersample_factor: int = 4) -> Callable: +def supersampled(supersample_factor: int = 4) -> Callable[[Callable[..., None]], Callable[..., None]]: # Anti-alias the drawing performed by the wrapped function by # using supersampling - def create_wrapper(f: Callable) -> Callable: + def create_wrapper(f: Callable[..., None]) -> Callable[..., None]: @wraps(f) def supersampled_wrapper(buf: BufType, width: int, height: int, *args: Any, **kw: Any) -> None: w, h = supersample_factor * width, supersample_factor * height @@ -781,7 +781,7 @@ def braille(buf: BufType, width: int, height: int, which: int = 0) -> None: braille_dot(buf, width, height, col, row) -box_chars: Dict[str, List[Callable]] = { +box_chars: Dict[str, List[Callable[[BufType, int, int], Any]]] = { '─': [hline], '━': [p(hline, level=3)], '│': [vline], @@ -1008,7 +1008,7 @@ for starts, func, pattern in ( for chars, func_ in (('╒╕╘╛', dvcorner), ('╓╖╙╜', dhcorner), ('╔╗╚╝', dcorner), ('╟╢╤╧', dpip)): for ch in chars: - box_chars[ch] = [p(cast(Callable, func_), which=ch)] + box_chars[ch] = [p(func_, which=ch)] for i in range(256): box_chars[chr(0x2800 + i)] = [p(braille, which=i)] diff --git a/kitty/marks.py b/kitty/marks.py index b06d0b2c6..e7e53eaee 100644 --- a/kitty/marks.py +++ b/kitty/marks.py @@ -22,7 +22,7 @@ def get_output_variables(left_address: int, right_address: int, color_address: i ) -def marker_from_regex(expression: Union[str, Pattern], color: int, flags: int = re.UNICODE) -> MarkerFunc: +def marker_from_regex(expression: Union[str, 'Pattern[str]'], color: int, flags: int = re.UNICODE) -> MarkerFunc: color = max(1, min(color, 3)) if isinstance(expression, str): pat = re.compile(expression, flags=flags) diff --git a/kitty/notify.py b/kitty/notify.py index d3473c427..2ed16bae4 100644 --- a/kitty/notify.py +++ b/kitty/notify.py @@ -194,12 +194,12 @@ def reset_registry() -> None: id_counter = count() -def notify_with_command(cmd: NotificationCommand, window_id: int, notify: NotifyImplementation = notify_implementation) -> None: +def notify_with_command(cmd: NotificationCommand, window_id: int, notify_implementation: NotifyImplementation = notify_implementation) -> None: title = cmd.title or cmd.body body = cmd.body if cmd.title else '' if title: identifier = 'i' + str(next(id_counter)) - notify_implementation(title, body, identifier=identifier) + notify_implementation(title, body, identifier) register_identifier(identifier, cmd, window_id)