Add more type annotations

This commit is contained in:
Kovid Goyal 2021-10-27 08:45:45 +05:30
parent 4494ddd8ff
commit 9c2f96f7eb
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 36 additions and 27 deletions

View File

@ -245,7 +245,7 @@ def patch_file(path: str, what: str, text: str, start_marker: str = '/* ', end_m
f.write(raw) 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) 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='') 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.""" """Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n): for i in range(0, len(lst), n):
yield lst[i:i + n] yield lst[i:i + n]

View File

@ -6,7 +6,7 @@ import json
import os import os
import re import re
import sys import sys
from typing import Callable, Dict, List, Optional, Tuple from typing import Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple
_plat = sys.platform.lower() _plat = sys.platform.lower()
is_linux = 'linux' in _plat is_linux = 'linux' in _plat
@ -14,6 +14,21 @@ is_openbsd = 'openbsd' in _plat
base = os.path.dirname(os.path.abspath(__file__)) 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: class Env:
cc: List[str] = [] 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) 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 = env.copy()
ans.cflags.append('-fPIC') ans.cflags.append('-fPIC')
ans.cppflags.append('-D_GLFW_' + module.upper()) ans.cppflags.append('-D_GLFW_' + module.upper())

View File

@ -107,7 +107,7 @@ else:
self._override = RunOnce self._override = RunOnce
def run_once(f: Callable[[], _T]) -> RunOnce: def run_once(f: Callable[[], _T]) -> RunOnce[_T]:
return RunOnce(f) return RunOnce(f)

View File

@ -20,11 +20,12 @@ from contextlib import suppress
from functools import lru_cache, partial from functools import lru_cache, partial
from pathlib import Path from pathlib import Path
from typing import ( from typing import (
Callable, Dict, Iterable, Iterator, List, NamedTuple, Optional, Sequence, Callable, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple,
Set, Tuple, Union Union
) )
from glfw import glfw # noqa from glfw import glfw
from glfw.glfw import Command, CompileKey
if sys.version_info[:2] < (3, 6): if sys.version_info[:2] < (3, 6):
raise SystemExit('kitty requires python >= 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') 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: def emphasis(text: str) -> str:
if sys.stdout.isatty(): if sys.stdout.isatty():
text = '\033[32m' + text + '\033[39m' 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))) 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([ ver = subprocess.check_output([
PKGCONFIG, package, '--modversion']).decode('utf-8').strip() PKGCONFIG, package, '--modversion']).decode('utf-8').strip()
m = re.match(r'(\d+).(\d+)', ver) m = re.match(r'(\d+).(\d+)', ver)
if m is not None: if m is not None:
qmajor, qminor = map(int, m.groups()) qmajor, qminor = map(int, m.groups())
return qmajor, qminor return qmajor, qminor
return None return -1, -1
def at_least_version(package: str, major: int, minor: int = 0) -> None: 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: except Exception:
num_workers = 2 num_workers = 2
items = list(reversed(items)) 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 failed = None
num, total = 0, len(items) num, total = 0, len(items)
@ -613,9 +600,9 @@ class CompilationDatabase:
self, self,
desc: str, desc: str,
cmd: List[str], cmd: List[str],
is_newer_func: Callable, is_newer_func: Callable[[], bool],
key: Optional[CompileKey] = None, key: Optional[CompileKey] = None,
on_success: Optional[Callable] = None, on_success: Optional[Callable[[], None]] = None,
keyfile: Optional[str] = None keyfile: Optional[str] = None
) -> None: ) -> None:
def no_op() -> None: def no_op() -> None: