Skip to content

Commit

Permalink
refactor: make latest ruff/mypy happy
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Jan 3, 2025
1 parent ea756cb commit 6a2f6cb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
8 changes: 6 additions & 2 deletions plugin/cache.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand Down
3 changes: 2 additions & 1 deletion plugin/commands/auto_set_syntax_debug_information.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 6 additions & 3 deletions plugin/listener.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions plugin/rules/constraint.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions plugin/rules/constraints/is_interpreter.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion plugin/settings.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 8 additions & 4 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])


Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6a2f6cb

Please sign in to comment.