Add more type annotations

This commit is contained in:
Kovid Goyal
2021-10-27 11:13:28 +05:30
parent 6f19fd5912
commit 69e903a4c4
6 changed files with 34 additions and 28 deletions

View File

@@ -23,10 +23,9 @@ class Command(NamedTuple):
desc: str
cmd: Sequence[str]
is_newer_func: Callable[[], bool]
on_success: Callable[[], None]
key: Optional[CompileKey]
keyfile: Optional[str]
on_success: Callable[[], None] = lambda: None
key: Optional[CompileKey] = None
keyfile: Optional[str] = None
class Env:
@@ -140,7 +139,13 @@ def init_env(
return ans
def build_wayland_protocols(env: Env, Command: Callable, parallel_run: Callable, emphasis: Callable, newer: Callable, dest_dir: str) -> None:
def build_wayland_protocols(
env: Env,
parallel_run: Callable[[List[Command]], None],
emphasis: Callable[[str], str],
newer: Callable[..., bool],
dest_dir: str
) -> None:
items = []
for protocol in env.wayland_protocols:
src = os.path.join(env.wayland_packagedir, protocol)
@@ -153,8 +158,7 @@ def build_wayland_protocols(env: Env, Command: Callable, parallel_run: Callable,
q = 'client-header' if ext == 'h' else env.wayland_scanner_code
items.append(Command(
'Generating {} ...'.format(emphasis(os.path.basename(dest))),
[env.wayland_scanner, q, src, dest],
lambda: True, None, None, None))
[env.wayland_scanner, q, src, dest], lambda: True))
if items:
parallel_run(items)

View File

@@ -12,9 +12,10 @@ from typing import (
)
from kitty.conf.types import Definition, MultiOption, Option, unset
from kitty.types import _T
def chunks(lst: List, n: int) -> Iterator[List]:
def chunks(lst: List[_T], n: int) -> Iterator[List[_T]]:
for i in range(0, len(lst), n):
yield lst[i:i + n]
@@ -51,7 +52,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
ans = ans.replace('NoneType', 'None')
return ans
def option_type_data(option: Union[Option, MultiOption]) -> Tuple[Callable, str]:
def option_type_data(option: Union[Option, MultiOption]) -> Tuple[Callable[[Any], Any], str]:
func = option.parser_func
if func.__module__ == 'builtins':
return func, func.__name__
@@ -270,7 +271,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
for aname, func in action_parsers.items():
a(f'defaults.{aname} = [')
only: Dict[str, List[Tuple[str, Callable]]] = {}
only: Dict[str, List[Tuple[str, Callable[..., Any]]]] = {}
for sc in defn.iter_all_maps(aname):
if not sc.add_to_default:
continue
@@ -333,7 +334,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
preamble = ['# generated by gen-config.py DO NOT edit', '']
a = preamble.append
def output_imports(imports: Set, add_module_imports: bool = True) -> None:
def output_imports(imports: Set[Tuple[str, str]], add_module_imports: bool = True) -> None:
a('import typing')
seen_mods = {'typing'}
mmap: Dict[str, List[str]] = {}

View File

@@ -3,7 +3,7 @@
from contextlib import suppress
from typing import (
TYPE_CHECKING, Any, Callable, Dict, FrozenSet, Generator, Iterable, List,
TYPE_CHECKING, Any, Callable, Dict, FrozenSet, Iterable, Iterator, List,
NoReturn, Optional, Tuple, Type, Union, cast
)
@@ -64,9 +64,9 @@ class PayloadGetter:
no_response = NoResponse()
payload_get = object()
ResponseType = Optional[Union[bool, str]]
CmdReturnType = Union[Dict[str, Any], List, Tuple, str, int, float, bool]
CmdGenerator = Generator[CmdReturnType, None, None]
ResponseType = Union[bool, str, None]
CmdReturnType = Union[Dict[str, Any], List[Any], Tuple[Any, ...], str, int, float, bool]
CmdGenerator = Iterator[CmdReturnType]
PayloadType = Optional[Union[CmdReturnType, CmdGenerator]]
PayloadGetType = PayloadGetter
ArgsType = List[str]
@@ -120,7 +120,7 @@ class RemoteCommand:
args_count: Optional[int] = None
args_completion: Optional[Dict[str, Tuple[str, Union[Callable[[], Iterable[str]], Tuple[str, ...]]]]] = None
defaults: Optional[Dict[str, Any]] = None
options_class: Type = RCOptions
options_class: Type[RCOptions] = RCOptions
def __init__(self) -> None:
self.desc = self.desc or self.short_desc

View File

@@ -4,15 +4,16 @@
import base64
import os
import sys
from typing import TYPE_CHECKING, Dict, Generator, List, Optional, Union
from typing import TYPE_CHECKING, List, Optional, Union
from kitty.fast_data_types import KeyEvent as WindowSystemKeyEvent
from kitty.key_encoding import decode_key_event_as_window_system_key
from kitty.options.utils import parse_send_text_bytes
from .base import (
MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, MatchError,
PayloadGetType, PayloadType, RCOptions, RemoteCommand, ResponseType, Window
MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, CmdGenerator,
MatchError, PayloadGetType, PayloadType, RCOptions, RemoteCommand,
ResponseType, Window
)
if TYPE_CHECKING:
@@ -63,7 +64,7 @@ Do not send text to the active window, even if it is one of the matched windows.
limit = 1024
ret = {'match': opts.match, 'data': '', 'match_tab': opts.match_tab, 'all': opts.all, 'exclude_active': opts.exclude_active}
def pipe() -> Generator[Dict, None, None]:
def pipe() -> CmdGenerator:
if sys.stdin.isatty():
ret['exclude_active'] = True
import select
@@ -90,14 +91,14 @@ Do not send text to the active window, even if it is one of the matched windows.
ret['data'] = 'base64:' + base64.standard_b64encode(data).decode('ascii')
yield ret
def chunks(text: str) -> Generator[Dict, None, None]:
def chunks(text: str) -> CmdGenerator:
data = parse_send_text_bytes(text).decode('utf-8')
while data:
ret['data'] = 'text:' + data[:limit]
yield ret
data = data[limit:]
def file_pipe(path: str) -> Generator[Dict, None, None]:
def file_pipe(path: str) -> CmdGenerator:
with open(path, 'rb') as f:
while True:
data = f.read(limit)
@@ -116,7 +117,7 @@ Do not send text to the active window, even if it is one of the matched windows.
text = ' '.join(args)
sources.append(chunks(text))
def chain() -> Generator[Dict, None, None]:
def chain() -> CmdGenerator:
for src in sources:
yield from src
return chain()

View File

@@ -4,12 +4,12 @@
import imghdr
import tempfile
from base64 import standard_b64decode, standard_b64encode
from typing import IO, TYPE_CHECKING, Dict, Generator, Optional
from typing import IO, TYPE_CHECKING, Optional
from uuid import uuid4
from .base import (
MATCH_WINDOW_OPTION, ArgsType, Boss, PayloadGetType, PayloadType,
RCOptions, RemoteCommand, ResponseType, Window
MATCH_WINDOW_OPTION, ArgsType, Boss, CmdGenerator, PayloadGetType,
PayloadType, RCOptions, RemoteCommand, ResponseType, Window
)
if TYPE_CHECKING:
@@ -83,7 +83,7 @@ failed, the command will exit with a success code.
if imghdr.what(path) != 'png':
self.fatal(f'{path} is not a PNG image')
def file_pipe(path: str) -> Generator[Dict, None, None]:
def file_pipe(path: str) -> CmdGenerator:
with open(path, 'rb') as f:
while True:
data = f.read(512)

View File

@@ -748,7 +748,7 @@ def compile_glfw(compilation_database: CompilationDatabase) -> None:
all_headers = [os.path.join('glfw', x) for x in genv.all_headers]
if module == 'wayland':
try:
glfw.build_wayland_protocols(genv, Command, parallel_run, emphasis, newer, 'glfw')
glfw.build_wayland_protocols(genv, parallel_run, emphasis, newer, 'glfw')
except SystemExit as err:
print(err, file=sys.stderr)
print(error('Disabling building of wayland backend'), file=sys.stderr)