Skip to content

Commit abedd30

Browse files
committed
Use ParamSpec to specify api function types
1 parent bc83517 commit abedd30

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

.mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[mypy]
22
strict_optional = true
3-
plugins = duet.typing
43
show_error_codes = true
54
warn_unused_ignores = true
65

duet/api.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@
3232
TypeVar,
3333
)
3434

35+
from typing_extensions import ParamSpec
36+
3537
import duet.impl as impl
3638
from duet.aitertools import aenumerate, aiter, AnyIterable, AsyncCollector
3739
from duet.futuretools import AwaitableFuture
3840

41+
P = ParamSpec("P")
3942
T = TypeVar("T")
4043
U = TypeVar("U")
4144

4245

43-
def run(func: Callable[..., Awaitable[T]], *args, **kwds) -> T:
46+
def run(func: Callable[P, Awaitable[T]], *args: P.args, **kwds: P.kwargs) -> T:
4447
"""Run an async function to completion.
4548
4649
Args:
@@ -72,7 +75,7 @@ def run(func: Callable[..., Awaitable[T]], *args, **kwds) -> T:
7275
scheduler.cleanup_signals()
7376

7477

75-
def sync(f: Callable[..., Awaitable[T]]) -> Callable[..., T]:
78+
def sync(f: Callable[P, Awaitable[T]]) -> Callable[P, T]:
7679
"""Decorator that adds a sync version of async function or method."""
7780
if isinstance(f, classmethod):
7881
raise TypeError(f"duet.sync cannot be applied to classmethod {f.__func__}")
@@ -113,7 +116,7 @@ def wrapped(self, *args, **kw):
113116
def wrapped(*args, **kw):
114117
return run(f, *args, **kw)
115118

116-
return wrapped
119+
return wrapped # type: ignore[return-value]
117120

118121

119122
def awaitable(value):
@@ -375,12 +378,14 @@ def __init__(
375378
def cancel(self) -> None:
376379
self._main_task.interrupt(self._main_task, CancelledError())
377380

378-
def spawn(self, func: Callable[..., Awaitable[Any]], *args, **kwds) -> None:
381+
def spawn(self, func: Callable[P, Awaitable[Any]], *args: P.args, **kwds: P.kwargs) -> None:
379382
"""Starts a background task that will run the given function."""
380383
task = self._scheduler.spawn(self._run(func, *args, **kwds), main_task=self._main_task)
381384
self._tasks.add(task)
382385

383-
async def _run(self, func: Callable[..., Awaitable[Any]], *args, **kwds) -> None:
386+
async def _run(
387+
self, func: Callable[P, Awaitable[Any]], *args: P.args, **kwds: P.kwargs
388+
) -> None:
384389
task = impl.current_task()
385390
try:
386391
await func(*args, **kwds)
@@ -513,7 +518,7 @@ def scope(self) -> Scope:
513518
def limiter(self) -> Limiter:
514519
pass
515520

516-
def spawn(self, func: Callable[..., Awaitable[Any]], *args, **kwds) -> None:
521+
def spawn(self, func: Callable[P, Awaitable[Any]], *args: P.args, **kwds: P.kwargs) -> None:
517522
"""Starts a background task that will run the given function."""
518523
self.scope.spawn(func, *args, **kwds)
519524

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
typing-extensions >= 3.10.0; python_version <= '3.7'
1+
typing-extensions >= 4.0.0; python_version < '3.10'

0 commit comments

Comments
 (0)