From 726d4ca9bc67601941a0bbdbe4d6a8fda6446690 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 9 Oct 2023 11:56:25 +0530 Subject: [PATCH] Typecheck map_type --- kitty/open_actions.py | 4 ++-- kitty/options/utils.py | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/kitty/open_actions.py b/kitty/open_actions.py index 9b1a1d567..905cbfb51 100644 --- a/kitty/open_actions.py +++ b/kitty/open_actions.py @@ -13,7 +13,7 @@ from .conf.utils import KeyAction, to_cmdline_implementation from .constants import config_dir from .fast_data_types import get_options from .guess_mime_type import guess_type -from .options.utils import ActionAlias, resolve_aliases_and_parse_actions +from .options.utils import ActionAlias, MapType, resolve_aliases_and_parse_actions from .types import run_once from .typing import MatchType from .utils import expandvars, get_editor, log_error, resolved_shell @@ -79,7 +79,7 @@ def parse(lines: Iterable[str]) -> Iterator[OpenAction]: for (mc, action_defns) in entries: actions: List[KeyAction] = [] for defn in action_defns: - actions.extend(resolve_aliases_and_parse_actions(defn, alias_map, 'open_action')) + actions.extend(resolve_aliases_and_parse_actions(defn, alias_map, MapType.OPEN_ACTION)) yield OpenAction(mc, tuple(actions)) diff --git a/kitty/options/utils.py b/kitty/options/utils.py index 29ee951ad..99f9b962c 100644 --- a/kitty/options/utils.py +++ b/kitty/options/utils.py @@ -2,6 +2,7 @@ # License: GPLv3 Copyright: 2021, Kovid Goyal +import enum import re import sys from functools import lru_cache @@ -50,6 +51,12 @@ func_with_args = KeyFuncWrapper[FuncArgsType]() DELETE_ENV_VAR = '_delete_this_env_var_' +class MapType(enum.Enum): + MAP = 'map' + MOUSE_MAP = 'mouse_map' + OPEN_ACTION = 'open_action' + + class InvalidMods(ValueError): pass @@ -930,7 +937,7 @@ def narrow_symbols(val: str) -> Iterable[Tuple[Tuple[int, int], int]]: yield x, int(y or 1) -def parse_key_action(action: str, action_type: str = 'map') -> KeyAction: +def parse_key_action(action: str, action_type: MapType = MapType.MAP) -> KeyAction: parts = action.strip().split(maxsplit=1) func = parts[0] if len(parts) == 1: @@ -961,7 +968,7 @@ class AliasMap: self.aliases.update(aa.aliases) @lru_cache(maxsize=256) - def resolve_aliases(self, definition: str, map_type: str = 'map') -> Tuple[KeyAction, ...]: + def resolve_aliases(self, definition: str, map_type: MapType = MapType.MAP) -> Tuple[KeyAction, ...]: return tuple(resolve_aliases_and_parse_actions(definition, self.aliases, map_type)) @@ -977,7 +984,7 @@ def build_action_aliases(raw: Dict[str, str], first_arg_replacement: str = '') - def resolve_aliases_and_parse_actions( - defn: str, aliases: Dict[str, List[ActionAlias]], map_type: str + defn: str, aliases: Dict[str, List[ActionAlias]], map_type: MapType ) -> Iterator[KeyAction]: parts = defn.split(maxsplit=1) if len(parts) == 1: @@ -1017,7 +1024,7 @@ def resolve_aliases_and_parse_actions( class BaseDefinition: no_op_actions = frozenset(('noop', 'no-op', 'no_op')) - map_type: str = 'map' + map_type: MapType = MapType.MAP definition_location: CurrentlyParsing def __init__(self, definition: str = '') -> None: @@ -1045,7 +1052,7 @@ def resolve_key_mods(kitty_mod: int, mods: int) -> int: class MouseMapping(BaseDefinition): - map_type: str = 'mouse_map' + map_type: MapType = MapType.MOUSE_MAP def __init__( self, button: int = 0, mods: int = 0, repeat_count: int = 1, grabbed: bool = False,