Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove cast() use ParamSpec #353

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions babi/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from collections.abc import Generator
from re import Match
from re import Pattern
from typing import Any
from typing import cast
from typing import Concatenate
from typing import IO
from typing import Literal
from typing import NamedTuple
from typing import ParamSpec
from typing import TYPE_CHECKING
from typing import TypedDict
from typing import TypeVar
Expand All @@ -37,7 +37,9 @@
if TYPE_CHECKING:
from babi.main import Screen # XXX: circular

TCallable = TypeVar('TCallable', bound=Callable[..., Any])
P = ParamSpec('P')
R = TypeVar('R')
FileMethod = Callable[Concatenate['File', P], R]

WS_RE = re.compile(r'^\s*')

Expand Down Expand Up @@ -127,43 +129,55 @@ def apply(self, file: File) -> Action:
return action


def action(func: TCallable) -> TCallable:
def action(func: FileMethod[P, R]) -> FileMethod[P, R]:
@functools.wraps(func)
def action_inner(self: File, *args: Any, **kwargs: Any) -> Any:
def action_inner(self: File, *args: P.args, **kwargs: P.kwargs) -> R:
self.finalize_previous_action()
return func(self, *args, **kwargs)
return cast(TCallable, action_inner)
return action_inner


def edit_action(
name: str,
*,
final: bool,
) -> Callable[[TCallable], TCallable]:
def edit_action_decorator(func: TCallable) -> TCallable:
) -> Callable[[FileMethod[P, R]], FileMethod[P, R]]:
def edit_action_decorator(func: FileMethod[P, R]) -> FileMethod[P, R]:
@functools.wraps(func)
def edit_action_inner(self: File, *args: Any, **kwargs: Any) -> Any:
def edit_action_inner(
self: File,
*args: P.args,
**kwargs: P.kwargs,
) -> R:
with self.edit_action_context(name, final=final):
return func(self, *args, **kwargs)
return cast(TCallable, edit_action_inner)
return edit_action_inner
return edit_action_decorator


def keep_selection(func: TCallable) -> TCallable:
def keep_selection(func: FileMethod[P, R]) -> FileMethod[P, R]:
@functools.wraps(func)
def keep_selection_inner(self: File, *args: Any, **kwargs: Any) -> Any:
def keep_selection_inner(
self: File,
*args: P.args,
**kwargs: P.kwargs,
) -> R:
with self.select():
return func(self, *args, **kwargs)
return cast(TCallable, keep_selection_inner)
return keep_selection_inner


def clear_selection(func: TCallable) -> TCallable:
def clear_selection(func: FileMethod[P, R]) -> FileMethod[P, R]:
@functools.wraps(func)
def clear_selection_inner(self: File, *args: Any, **kwargs: Any) -> Any:
def clear_selection_inner(
self: File,
*args: P.args,
**kwargs: P.kwargs,
) -> R:
ret = func(self, *args, **kwargs)
self.selection.clear()
return ret
return cast(TCallable, clear_selection_inner)
return clear_selection_inner


class Found(NamedTuple):
Expand Down
Loading