From 9c2f96f7eb13e5bcdc4f184454be8091beb892ce Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Oct 2021 08:45:45 +0530 Subject: [PATCH] Add more type annotations --- gen-key-constants.py | 4 ++-- glfw/glfw.py | 26 ++++++++++++++++++++++++-- kitty/types.py | 2 +- setup.py | 31 +++++++++---------------------- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/gen-key-constants.py b/gen-key-constants.py index 1593804eb..88f570c15 100755 --- a/gen-key-constants.py +++ b/gen-key-constants.py @@ -245,7 +245,7 @@ def patch_file(path: str, what: str, text: str, start_marker: str = '/* ', end_m f.write(raw) -def serialize_dict(x: dict) -> str: +def serialize_dict(x: Dict[Any, Any]) -> str: return pformat(x, indent=4).replace('{', '{\n ', 1) @@ -349,7 +349,7 @@ def generate_legacy_text_key_maps() -> None: patch_file('kitty_tests/keys.py', 'legacy letter tests', '\n'.join(tests), start_marker='# ', end_marker='') -def chunks(lst: List, n: int) -> Any: +def chunks(lst: List[Any], n: int) -> Any: """Yield successive n-sized chunks from lst.""" for i in range(0, len(lst), n): yield lst[i:i + n] diff --git a/glfw/glfw.py b/glfw/glfw.py index cded9f4f4..25d85f38a 100755 --- a/glfw/glfw.py +++ b/glfw/glfw.py @@ -6,7 +6,7 @@ import json import os import re import sys -from typing import Callable, Dict, List, Optional, Tuple +from typing import Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple _plat = sys.platform.lower() is_linux = 'linux' in _plat @@ -14,6 +14,21 @@ is_openbsd = 'openbsd' in _plat base = os.path.dirname(os.path.abspath(__file__)) +class CompileKey(NamedTuple): + src: str + dest: str + + +class Command(NamedTuple): + desc: str + cmd: Sequence[str] + is_newer_func: Callable[[], bool] + on_success: Callable[[], None] + key: Optional[CompileKey] + keyfile: Optional[str] + + + class Env: cc: List[str] = [] @@ -56,7 +71,14 @@ def wayland_protocol_file_name(base: str, ext: str = 'c') -> str: return 'wayland-{}-client-protocol.{}'.format(base, ext) -def init_env(env: Env, pkg_config: Callable, pkg_version: Callable, at_least_version: Callable, test_compile: Callable, module: str = 'x11') -> Env: +def init_env( + env: Env, + pkg_config: Callable[..., List[str]], + pkg_version: Callable[[str], Tuple[int, int]], + at_least_version: Callable[..., None], + test_compile: Callable[..., bool], + module: str = 'x11' +) -> Env: ans = env.copy() ans.cflags.append('-fPIC') ans.cppflags.append('-D_GLFW_' + module.upper()) diff --git a/kitty/types.py b/kitty/types.py index d863cec57..c9f489e6b 100644 --- a/kitty/types.py +++ b/kitty/types.py @@ -107,7 +107,7 @@ else: self._override = RunOnce -def run_once(f: Callable[[], _T]) -> RunOnce: +def run_once(f: Callable[[], _T]) -> RunOnce[_T]: return RunOnce(f) diff --git a/setup.py b/setup.py index 1e5d11373..3814c3006 100755 --- a/setup.py +++ b/setup.py @@ -20,11 +20,12 @@ from contextlib import suppress from functools import lru_cache, partial from pathlib import Path from typing import ( - Callable, Dict, Iterable, Iterator, List, NamedTuple, Optional, Sequence, - Set, Tuple, Union + Callable, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, + Union ) -from glfw import glfw # noqa +from glfw import glfw +from glfw.glfw import Command, CompileKey if sys.version_info[:2] < (3, 6): raise SystemExit('kitty requires python >= 3.6') @@ -79,20 +80,6 @@ class Options(argparse.Namespace): canberra_library: Optional[str] = os.getenv('KITTY_CANBERRA_LIBRARY') -class CompileKey(NamedTuple): - src: str - dest: str - - -class Command(NamedTuple): - desc: str - cmd: Sequence[str] - is_newer_func: Callable[[], bool] - on_success: Callable[[], None] - key: Optional[CompileKey] - keyfile: Optional[str] - - def emphasis(text: str) -> str: if sys.stdout.isatty(): text = '\033[32m' + text + '\033[39m' @@ -120,14 +107,14 @@ def pkg_config(pkg: str, *args: str) -> List[str]: raise SystemExit('The package {} was not found on your system'.format(error(pkg))) -def pkg_version(package: str) -> Optional[Tuple[int, int]]: +def pkg_version(package: str) -> Tuple[int, int]: ver = subprocess.check_output([ PKGCONFIG, package, '--modversion']).decode('utf-8').strip() m = re.match(r'(\d+).(\d+)', ver) if m is not None: qmajor, qminor = map(int, m.groups()) return qmajor, qminor - return None + return -1, -1 def at_least_version(package: str, major: int, minor: int = 0) -> None: @@ -559,7 +546,7 @@ def parallel_run(items: List[Command]) -> None: except Exception: num_workers = 2 items = list(reversed(items)) - workers: Dict[int, Tuple[Optional[Command], Optional[subprocess.Popen]]] = {} + workers: Dict[int, Tuple[Optional[Command], Optional['subprocess.Popen[bytes]']]] = {} failed = None num, total = 0, len(items) @@ -613,9 +600,9 @@ class CompilationDatabase: self, desc: str, cmd: List[str], - is_newer_func: Callable, + is_newer_func: Callable[[], bool], key: Optional[CompileKey] = None, - on_success: Optional[Callable] = None, + on_success: Optional[Callable[[], None]] = None, keyfile: Optional[str] = None ) -> None: def no_op() -> None: