From 1567f36431ace625ce73e211fbd31319d7aaea29 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sat, 4 Jan 2025 02:41:10 +0800 Subject: [PATCH] refactor: make latest ruff/mypy happy Signed-off-by: Jack Cherng --- plugin/cache.py | 8 ++++++-- .../commands/auto_set_syntax_debug_information.py | 3 ++- plugin/listener.py | 9 ++++++--- plugin/rules/constraint.py | 5 +++-- plugin/rules/constraints/is_interpreter.py | 5 +++-- plugin/settings.py | 3 ++- plugin/utils.py | 12 ++++++++---- pyproject.toml | 15 +++++---------- 8 files changed, 35 insertions(+), 25 deletions(-) diff --git a/plugin/cache.py b/plugin/cache.py index fe79d7df..899d124a 100644 --- a/plugin/cache.py +++ b/plugin/cache.py @@ -1,11 +1,15 @@ from __future__ import annotations +from collections.abc import Callable from functools import _lru_cache_wrapper, lru_cache -from typing import Any, Callable, TypeVar, cast +from typing import TYPE_CHECKING, Any, TypeVar, cast _cached_functions: set[_lru_cache_wrapper] = set() -_T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) +if TYPE_CHECKING: + _T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) +else: + _T_Callable = TypeVar("_T_Callable", bound=Callable) def clearable_lru_cache(*args: Any, **kwargs: Any) -> Callable[[_T_Callable], _T_Callable]: diff --git a/plugin/commands/auto_set_syntax_debug_information.py b/plugin/commands/auto_set_syntax_debug_information.py index 0d665769..a6133d96 100644 --- a/plugin/commands/auto_set_syntax_debug_information.py +++ b/plugin/commands/auto_set_syntax_debug_information.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import Any, Mapping +from collections.abc import Mapping +from typing import Any import sublime_plugin diff --git a/plugin/listener.py b/plugin/listener.py index e8f7f0b7..73991708 100644 --- a/plugin/listener.py +++ b/plugin/listener.py @@ -1,8 +1,8 @@ from __future__ import annotations -from collections.abc import Iterable, Sequence +from collections.abc import Callable, Iterable, Sequence from functools import wraps -from typing import Any, Callable, TypeVar, cast +from typing import TYPE_CHECKING, Any, TypeVar, cast import sublime import sublime_plugin @@ -17,7 +17,10 @@ from .types import ListenerEvent from .utils import debounce, is_transient_view, stringify -_T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) +if TYPE_CHECKING: + _T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) +else: + _T_Callable = TypeVar("_T_Callable", bound=Callable) def set_up_window(window: sublime.Window) -> None: diff --git a/plugin/rules/constraint.py b/plugin/rules/constraint.py index d5102506..e1e98d08 100644 --- a/plugin/rules/constraint.py +++ b/plugin/rules/constraint.py @@ -1,11 +1,12 @@ from __future__ import annotations import operator +import re from abc import ABC, abstractmethod from collections.abc import Callable, Generator, Iterable from dataclasses import dataclass, field from pathlib import Path -from typing import Any, Pattern, TypeVar, final +from typing import Any, TypeVar, final from more_itertools import first_true from typing_extensions import Self @@ -150,7 +151,7 @@ def _handled_comparator(comparator: str) -> Callable[[Any, Any], bool] | None: @final @staticmethod - def _handled_regex(args: tuple[Any, ...], kwargs: dict[str, Any]) -> Pattern[str]: + def _handled_regex(args: tuple[Any, ...], kwargs: dict[str, Any]) -> re.Pattern[str]: """Returns compiled regex object from `args` and `kwargs.regex_flags`.""" return compile_regex( merge_regexes(args), diff --git a/plugin/rules/constraints/is_interpreter.py b/plugin/rules/constraints/is_interpreter.py index 0d39ad94..732d2a20 100644 --- a/plugin/rules/constraints/is_interpreter.py +++ b/plugin/rules/constraints/is_interpreter.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import Any, Pattern, final +import re +from typing import Any, final from ...snapshot import ViewSnapshot from ...utils import compile_regex, merge_literals_to_regex, merge_regexes @@ -19,7 +20,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: if self.loosy_version: interpreters_regex = rf"(?:{interpreters_regex}(?:[\-_]?\d+(?:\.\d+)*)?)" - self.first_line_regex: Pattern[str] = compile_regex( + self.first_line_regex: re.Pattern[str] = compile_regex( merge_regexes(( # shebang rf"^#!(?:.+)\b{interpreters_regex}\b", diff --git a/plugin/settings.py b/plugin/settings.py index c07474e8..06702123 100644 --- a/plugin/settings.py +++ b/plugin/settings.py @@ -1,8 +1,9 @@ from __future__ import annotations from collections import ChainMap +from collections.abc import Callable from itertools import chain -from typing import Any, Callable, List, Mapping, MutableMapping +from typing import Any, List, Mapping, MutableMapping import sublime import sublime_plugin diff --git a/plugin/utils.py b/plugin/utils.py index b16fad8e..08ccb120 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -9,10 +9,10 @@ import sys import tempfile import threading -from collections.abc import Generator, Iterable, Mapping +from collections.abc import Callable, Generator, Iterable, Mapping from functools import cmp_to_key, lru_cache, reduce, wraps from pathlib import Path -from typing import Any, Callable, Pattern, TypeVar, Union, cast +from typing import TYPE_CHECKING, Any, Pattern, TypeVar, Union, cast import sublime from more_itertools import first_true, unique_everseen @@ -24,7 +24,11 @@ _T = TypeVar("_T") -_T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) +if TYPE_CHECKING: + _T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) +else: + _T_Callable = TypeVar("_T_Callable", bound=Callable) + _T_ExpandableVar = TypeVar("_T_ExpandableVar", bound=Union[None, bool, int, float, str, dict, list, tuple]) @@ -55,7 +59,7 @@ def remove_suffix(s: str, suffix: str) -> str: @clearable_lru_cache() -def compile_regex(regex: str | Pattern[str], flags: int = 0) -> Pattern[str]: +def compile_regex(regex: str | re.Pattern[str], flags: int = 0) -> re.Pattern[str]: """Compile the regex string/object into a object with the given flags.""" if isinstance(regex, Pattern): if regex.flags == flags: diff --git a/pyproject.toml b/pyproject.toml index 471444b3..8e80a2c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,10 @@ mypy_path = 'typings:stubs' python_version = '3.8' [[tool.mypy.overrides]] -module = ["plugin._vendor.*"] +module = [ + "magika.*", + "plugin._vendor.*", +] ignore_errors = true ignore_missing_imports = true @@ -44,15 +47,7 @@ exclude = [ ] [tool.ruff.lint] -select = [ - "E", - "F", - "W", - "I", - "UP", - "FURB", - "SIM", -] +select = ["E", "F", "W", "I", "UP", "FURB", "SIM"] ignore = ["E203"] [tool.ruff.lint.per-file-ignores]