Skip to content

Commit 5e68898

Browse files
authored
Merge pull request #353 from asottile/no-cast
remove cast() use ParamSpec
2 parents c10759c + 45f2f16 commit 5e68898

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

babi/file.py

+30-16
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
from collections.abc import Generator
1414
from re import Match
1515
from re import Pattern
16-
from typing import Any
17-
from typing import cast
16+
from typing import Concatenate
1817
from typing import IO
1918
from typing import Literal
2019
from typing import NamedTuple
20+
from typing import ParamSpec
2121
from typing import TYPE_CHECKING
2222
from typing import TypedDict
2323
from typing import TypeVar
@@ -37,7 +37,9 @@
3737
if TYPE_CHECKING:
3838
from babi.main import Screen # XXX: circular
3939

40-
TCallable = TypeVar('TCallable', bound=Callable[..., Any])
40+
P = ParamSpec('P')
41+
R = TypeVar('R')
42+
FileMethod = Callable[Concatenate['File', P], R]
4143

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

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

129131

130-
def action(func: TCallable) -> TCallable:
132+
def action(func: FileMethod[P, R]) -> FileMethod[P, R]:
131133
@functools.wraps(func)
132-
def action_inner(self: File, *args: Any, **kwargs: Any) -> Any:
134+
def action_inner(self: File, *args: P.args, **kwargs: P.kwargs) -> R:
133135
self.finalize_previous_action()
134136
return func(self, *args, **kwargs)
135-
return cast(TCallable, action_inner)
137+
return action_inner
136138

137139

138140
def edit_action(
139141
name: str,
140142
*,
141143
final: bool,
142-
) -> Callable[[TCallable], TCallable]:
143-
def edit_action_decorator(func: TCallable) -> TCallable:
144+
) -> Callable[[FileMethod[P, R]], FileMethod[P, R]]:
145+
def edit_action_decorator(func: FileMethod[P, R]) -> FileMethod[P, R]:
144146
@functools.wraps(func)
145-
def edit_action_inner(self: File, *args: Any, **kwargs: Any) -> Any:
147+
def edit_action_inner(
148+
self: File,
149+
*args: P.args,
150+
**kwargs: P.kwargs,
151+
) -> R:
146152
with self.edit_action_context(name, final=final):
147153
return func(self, *args, **kwargs)
148-
return cast(TCallable, edit_action_inner)
154+
return edit_action_inner
149155
return edit_action_decorator
150156

151157

152-
def keep_selection(func: TCallable) -> TCallable:
158+
def keep_selection(func: FileMethod[P, R]) -> FileMethod[P, R]:
153159
@functools.wraps(func)
154-
def keep_selection_inner(self: File, *args: Any, **kwargs: Any) -> Any:
160+
def keep_selection_inner(
161+
self: File,
162+
*args: P.args,
163+
**kwargs: P.kwargs,
164+
) -> R:
155165
with self.select():
156166
return func(self, *args, **kwargs)
157-
return cast(TCallable, keep_selection_inner)
167+
return keep_selection_inner
158168

159169

160-
def clear_selection(func: TCallable) -> TCallable:
170+
def clear_selection(func: FileMethod[P, R]) -> FileMethod[P, R]:
161171
@functools.wraps(func)
162-
def clear_selection_inner(self: File, *args: Any, **kwargs: Any) -> Any:
172+
def clear_selection_inner(
173+
self: File,
174+
*args: P.args,
175+
**kwargs: P.kwargs,
176+
) -> R:
163177
ret = func(self, *args, **kwargs)
164178
self.selection.clear()
165179
return ret
166-
return cast(TCallable, clear_selection_inner)
180+
return clear_selection_inner
167181

168182

169183
class Found(NamedTuple):

0 commit comments

Comments
 (0)