Skip to content

Commit c859398

Browse files
Update loop task factory typing with kwargs
For `PY311` the task factory will be called with the context kwarg. This is not correctly reflected in the typing for `Loop.set_task_factory` and `Loop.get_task_factory`. This commit addresses this with a protocol. Since `uvloop` requires a minimum Python 3.8 version, in which `typing.Protocol` is available; the more appropriate `typing.Unpack` for kwargs is introduced in Python 3.11.
1 parent 6c770dc commit c859398

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

uvloop/loop.pyi

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from typing import (
1111
Generator,
1212
List,
1313
Optional,
14+
Protocol,
1415
Sequence,
1516
Tuple,
1617
TypeVar,
@@ -24,6 +25,14 @@ _ExceptionHandler = Callable[[asyncio.AbstractEventLoop, _Context], Any]
2425
_SSLContext = Union[bool, None, ssl.SSLContext]
2526
_ProtocolT = TypeVar("_ProtocolT", bound=asyncio.BaseProtocol)
2627

28+
29+
class TaskFactoryCallable(Protocol):
30+
def __call__(
31+
self, loop: asyncio.AbstractEventLoop, coro: Generator[Any, None, _T], **kwargs: Any
32+
) -> asyncio.Future[_T]:
33+
...
34+
35+
2736
class Loop:
2837
def call_soon(
2938
self, callback: Callable[..., Any], *args: Any, context: Optional[Any] = ...
@@ -52,17 +61,8 @@ class Loop:
5261
*,
5362
name: Optional[str] = ...,
5463
) -> asyncio.Task[_T]: ...
55-
def set_task_factory(
56-
self,
57-
factory: Optional[
58-
Callable[[asyncio.AbstractEventLoop, Generator[Any, None, _T]], asyncio.Future[_T]]
59-
],
60-
) -> None: ...
61-
def get_task_factory(
62-
self,
63-
) -> Optional[
64-
Callable[[asyncio.AbstractEventLoop, Generator[Any, None, _T]], asyncio.Future[_T]]
65-
]: ...
64+
def set_task_factory(self, factory: Optional[TaskFactoryCallable]) -> None: ...
65+
def get_task_factory(self) -> Optional[TaskFactoryCallable]: ...
6666
@overload
6767
def run_until_complete(self, future: Generator[Any, None, _T]) -> _T: ...
6868
@overload

0 commit comments

Comments
 (0)