From 8f0825bc5c478b0eaf05e572e3f054686cd6e980 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 17 Dec 2021 05:41:27 +0530 Subject: [PATCH] Various fixes for mypy 0.920 --- kittens/icat/main.py | 6 ++++-- kittens/transfer/librsync.py | 2 +- kitty/boss.py | 7 +++---- kitty/conf/generate.py | 2 +- kitty/launch.py | 7 +++++-- kitty/window.py | 17 ++++++++++------- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/kittens/icat/main.py b/kittens/icat/main.py index 7bdf662b9..2507a28b5 100755 --- a/kittens/icat/main.py +++ b/kittens/icat/main.py @@ -389,7 +389,8 @@ def detect_support(wait_for: float = 10, silent: bool = False) -> bool: return 1 not in responses or 2 not in responses with NamedTemporaryFile() as f: - f.write(b'abcd'), f.flush() + f.write(b'abcd') + f.flush() gc = GraphicsCommand() gc.a = 'q' gc.s = gc.v = gc.i = 1 @@ -455,7 +456,8 @@ def process_single_item( try: if isinstance(item, bytes): tf = NamedTemporaryFile(prefix='stdin-image-data-', delete=False) - tf.write(item), tf.close() + tf.write(item) + tf.close() item = tf.name is_tempfile = True if url_pat is not None and url_pat.match(item) is not None: diff --git a/kittens/transfer/librsync.py b/kittens/transfer/librsync.py index 7b42adba2..1f951323d 100644 --- a/kittens/transfer/librsync.py +++ b/kittens/transfer/librsync.py @@ -113,7 +113,7 @@ class PatchFile(StreamingJob): self.overwrite_src = not output_path self.src_file = open(src_path, 'rb') if self.overwrite_src: - self.dest_file = tempfile.NamedTemporaryFile(mode='wb', dir=os.path.dirname(os.path.abspath(os.path.realpath(src_path))), delete=False) + self.dest_file: IO[bytes] = tempfile.NamedTemporaryFile(mode='wb', dir=os.path.dirname(os.path.abspath(os.path.realpath(src_path))), delete=False) else: self.dest_file = open(output_path, 'wb') job = begin_patch(self.read_from_src) diff --git a/kitty/boss.py b/kitty/boss.py index 9981996fb..257c746ef 100755 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -10,7 +10,7 @@ from functools import partial from gettext import gettext as _ from typing import ( Any, Callable, Container, Dict, Iterable, Iterator, List, Optional, Tuple, - Union, cast + Union ) from weakref import WeakValueDictionary @@ -518,9 +518,8 @@ class Boss: opts, items = parse_subcommand_cli(c, items) payload = c.message_to_kitty(global_opts, opts, items) import types - if isinstance(cast(types.GeneratorType, payload), types.GeneratorType): - payloads = cast(types.GeneratorType, payload) - for x in payloads: + if isinstance(payload, types.GeneratorType): + for x in payload: c.response_from_kitty(self, self.active_window, PayloadGetter(c, x if isinstance(x, dict) else {})) else: c.response_from_kitty(self, self.active_window, PayloadGetter(c, payload if isinstance(payload, dict) else {})) diff --git a/kitty/conf/generate.py b/kitty/conf/generate.py index 1a3943a6c..c5565aba6 100644 --- a/kitty/conf/generate.py +++ b/kitty/conf/generate.py @@ -110,7 +110,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]: else: func, typ = option_type_data(option) try: - params = inspect.signature(func).parameters + params = dict(inspect.signature(func).parameters) except Exception: params = {} if 'dict_with_parse_results' in params: diff --git a/kitty/launch.py b/kitty/launch.py index 38c3038f6..ec8eed5e0 100644 --- a/kitty/launch.py +++ b/kitty/launch.py @@ -210,7 +210,7 @@ def parse_launch_args(args: Optional[Sequence[str]] = None) -> LaunchSpec: return LaunchSpec(opts, args) -def get_env(opts: LaunchCLIOptions, active_child: Child) -> Dict[str, str]: +def get_env(opts: LaunchCLIOptions, active_child: Optional[Child]) -> Dict[str, str]: env: Dict[str, str] = {} if opts.copy_env and active_child: env.update(active_child.foreground_environ) @@ -308,7 +308,10 @@ def launch( force_target_tab: bool = False ) -> Optional[Window]: active = boss.active_window_for_cwd - active_child = getattr(active, 'child', None) + if active: + active_child = active.child + else: + active_child = None env = get_env(opts, active_child) kw: LaunchKwds = { 'allow_remote_control': opts.allow_remote_control, diff --git a/kitty/window.py b/kitty/window.py index 40d59bbb1..fd5cf8d3b 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -39,7 +39,7 @@ from .notify import NotificationCommand, handle_notification_cmd from .options.types import Options from .rgb import to_color from .terminfo import get_capabilities -from .types import MouseEvent, WindowGeometry, ac +from .types import MouseEvent, WindowGeometry, ac, run_once from .typing import BossType, ChildType, EdgeLiteral, TabType, TypedDict from .utils import ( get_primary_selection, load_shaders, log_error, open_cmd, open_url, @@ -50,6 +50,8 @@ MatchPatternType = Union[Pattern[str], Tuple[Pattern[str], Optional[Pattern[str] if TYPE_CHECKING: + import re + from .file_transmission import FileTransmission @@ -264,13 +266,14 @@ def setup_colors(screen: Screen, opts: Options) -> None: ) -def text_sanitizer(as_ansi: bool, add_wrap_markers: bool) -> Callable[[str], str]: - pat = getattr(text_sanitizer, 'pat', None) - if pat is None: - import re - pat = re.compile('\033\\[.*?m') - setattr(text_sanitizer, 'pat', pat) +@run_once +def text_sanitizer_pat() -> 're.Pattern[str]': + import re + return re.compile('\033\\[.*?m') + +def text_sanitizer(as_ansi: bool, add_wrap_markers: bool) -> Callable[[str], str]: + pat = text_sanitizer_pat() ansi, wrap_markers = not as_ansi, not add_wrap_markers def remove_wrap_markers(line: str) -> str: