From c899eb4ee3e66ebf0fac189e6ce7217a5ceb94e7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Oct 2021 13:44:12 +0530 Subject: [PATCH] Add more type annotations --- kitty/config.py | 4 ++-- kitty/notify.py | 14 ++++++++++---- kitty/tab_bar.py | 6 +++--- kitty/tabs.py | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/kitty/config.py b/kitty/config.py index b1978777c..fff56a019 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -56,9 +56,9 @@ def atomic_save(data: bytes, path: str) -> None: @contextmanager -def cached_values_for(name: str) -> Generator[Dict, None, None]: +def cached_values_for(name: str) -> Generator[Dict[str, Any], None, None]: cached_path = os.path.join(cache_dir(), name + '.json') - cached_values: Dict = {} + cached_values: Dict[str, Any] = {} try: with open(cached_path, 'rb') as f: cached_values.update(json.loads(f.read().decode('utf-8'))) diff --git a/kitty/notify.py b/kitty/notify.py index 93ae9ee45..d3473c427 100644 --- a/kitty/notify.py +++ b/kitty/notify.py @@ -10,6 +10,8 @@ from .constants import is_macos, logo_png_file from .fast_data_types import get_boss from .utils import log_error +NotifyImplementation = Callable[[str, str, str], None] + if is_macos: from .fast_data_types import cocoa_send_notification @@ -59,6 +61,10 @@ else: alloc_map[alloc_id] = identifier +def notify_implementation(title: str, body: str, identifier: str) -> None: + notify(title, body, identifier=identifier) + + class NotificationCommand: done: bool = True @@ -167,7 +173,7 @@ def register_identifier(identifier: str, cmd: NotificationCommand, window_id: in identifier_registry.popitem(False) -def notification_activated(identifier: str, activated_implementation: Optional[Callable] = None) -> None: +def notification_activated(identifier: str, activated_implementation: Optional[Callable[[str, int, bool, bool], None]] = None) -> None: if identifier == 'new-version': from .update_check import notification_activated as do do() @@ -188,12 +194,12 @@ def reset_registry() -> None: id_counter = count() -def notify_with_command(cmd: NotificationCommand, window_id: int, notify: Callable = notify) -> None: +def notify_with_command(cmd: NotificationCommand, window_id: int, notify: 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(title, body, identifier=identifier) + notify_implementation(title, body, identifier=identifier) register_identifier(identifier, cmd, window_id) @@ -202,7 +208,7 @@ def handle_notification_cmd( raw_data: str, window_id: int, prev_cmd: NotificationCommand, - notify_implementation: Callable = notify + notify_implementation: NotifyImplementation = notify_implementation ) -> Optional[NotificationCommand]: if osc_code == 99: cmd = merge_osc_99(prev_cmd, parse_osc_99(raw_data)) diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index bca7542d2..132bb40b6 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -4,7 +4,7 @@ import os from functools import lru_cache, partial, wraps from typing import ( - Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple + Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union ) from .borders import Border, BorderColor @@ -119,7 +119,7 @@ class Formatter: @run_once -def super_sub_maps() -> Tuple[dict, dict]: +def super_sub_maps() -> Tuple[Dict[int, Union[None, int]], Dict[int, Union[None, int]]]: import string sup_table = str.maketrans( string.ascii_lowercase + string.ascii_uppercase + string.digits + '+-=()', @@ -132,7 +132,7 @@ def super_sub_maps() -> Tuple[dict, dict]: class SupSub: - def __init__(self, data: dict, is_subscript: bool = False): + def __init__(self, data: Dict[str, Any], is_subscript: bool = False): self.__data = data self.__is_subscript = is_subscript diff --git a/kitty/tabs.py b/kitty/tabs.py index 08019ce6c..5c9ea1ae0 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -630,7 +630,7 @@ class Tab: # {{{ for w in self: yield w.as_dict(is_focused=w is active_window, is_self=w is self_window) - def matches(self, field: str, pat: Pattern) -> bool: + def matches(self, field: str, pat: 'Pattern[str]') -> bool: if field == 'id': return bool(pat.pattern == str(self.id)) if field == 'title':