Skip to content

Commit 4e6af84

Browse files
JelleZijlstragvanrossum
authored andcommitted
improve multiprocessing.Pool types (#2126)
Fixes #1703 Mostly introduce typevars to express that the return types match the return types of the callbacks. Removed comment about incompleteness since all documented classes from https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing.pool are covered.
1 parent 82c930e commit 4e6af84

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

stdlib/3/multiprocessing/pool.pyi

+35-37
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
# Stubs for multiprocessing.pool
2-
3-
# NOTE: These are incomplete!
4-
51
from typing import (
6-
Any, Callable, ContextManager, Iterable, Mapping, Optional, Dict, List,
7-
TypeVar,
2+
Any, Callable, ContextManager, Iterable, Mapping, Optional, List,
3+
TypeVar, Generic,
84
)
95

10-
_T = TypeVar('_T', bound='Pool')
6+
_PT = TypeVar('_PT', bound='Pool')
7+
_S = TypeVar('_S')
8+
_T = TypeVar('_T')
119

12-
class AsyncResult():
13-
def get(self, timeout: Optional[float] = ...) -> Any: ...
10+
class AsyncResult(Generic[_T]):
11+
def get(self, timeout: Optional[float] = ...) -> _T: ...
1412
def wait(self, timeout: Optional[float] = ...) -> None: ...
1513
def ready(self) -> bool: ...
1614
def successful(self) -> bool: ...
1715

1816
_IMIT = TypeVar('_IMIT', bound=IMapIterator)
1917

20-
class IMapIterator(Iterable[Any]):
18+
class IMapIterator(Iterable[_T]):
2119
def __iter__(self: _IMIT) -> _IMIT: ...
22-
def next(self, timeout: Optional[float] = ...) -> Any: ...
23-
def __next__(self, timeout: Optional[float] = ...) -> Any: ...
20+
def next(self, timeout: Optional[float] = ...) -> _T: ...
21+
def __next__(self, timeout: Optional[float] = ...) -> _T: ...
2422

2523
class Pool(ContextManager[Pool]):
2624
def __init__(self, processes: Optional[int] = ...,
@@ -29,46 +27,46 @@ class Pool(ContextManager[Pool]):
2927
maxtasksperchild: Optional[int] = ...,
3028
context: Optional[Any] = ...) -> None: ...
3129
def apply(self,
32-
func: Callable[..., Any],
30+
func: Callable[..., _T],
3331
args: Iterable[Any] = ...,
34-
kwds: Dict[str, Any] = ...) -> Any: ...
32+
kwds: Mapping[str, Any] = ...) -> _T: ...
3533
def apply_async(self,
36-
func: Callable[..., Any],
34+
func: Callable[..., _T],
3735
args: Iterable[Any] = ...,
38-
kwds: Dict[str, Any] = ...,
39-
callback: Optional[Callable[..., None]] = ...,
40-
error_callback: Optional[Callable[[BaseException], None]] = ...) -> AsyncResult: ...
36+
kwds: Mapping[str, Any] = ...,
37+
callback: Optional[Callable[[_T], None]] = ...,
38+
error_callback: Optional[Callable[[BaseException], None]] = ...) -> AsyncResult[_T]: ...
4139
def map(self,
42-
func: Callable[..., Any],
43-
iterable: Iterable[Any] = ...,
44-
chunksize: Optional[int] = ...) -> List[Any]: ...
45-
def map_async(self, func: Callable[..., Any],
46-
iterable: Iterable[Any] = ...,
40+
func: Callable[[_S], _T],
41+
iterable: Iterable[_S] = ...,
42+
chunksize: Optional[int] = ...) -> List[_T]: ...
43+
def map_async(self, func: Callable[[_S], _T],
44+
iterable: Iterable[_S] = ...,
4745
chunksize: Optional[int] = ...,
48-
callback: Optional[Callable[..., None]] = ...,
49-
error_callback: Optional[Callable[[BaseException], None]] = ...) -> AsyncResult: ...
46+
callback: Optional[Callable[[_T], None]] = ...,
47+
error_callback: Optional[Callable[[BaseException], None]] = ...) -> AsyncResult[List[_T]]: ...
5048
def imap(self,
51-
func: Callable[..., Any],
52-
iterable: Iterable[Any] = ...,
53-
chunksize: Optional[int] = ...) -> IMapIterator: ...
49+
func: Callable[[_S], _T],
50+
iterable: Iterable[_S] = ...,
51+
chunksize: Optional[int] = ...) -> IMapIterator[_T]: ...
5452
def imap_unordered(self,
55-
func: Callable[..., Any],
56-
iterable: Iterable[Any] = ...,
57-
chunksize: Optional[int] = ...) -> IMapIterator: ...
53+
func: Callable[[_S], _T],
54+
iterable: Iterable[_S] = ...,
55+
chunksize: Optional[int] = ...) -> IMapIterator[_T]: ...
5856
def starmap(self,
59-
func: Callable[..., Any],
57+
func: Callable[..., _T],
6058
iterable: Iterable[Iterable[Any]] = ...,
61-
chunksize: Optional[int] = ...) -> List[Any]: ...
59+
chunksize: Optional[int] = ...) -> List[_T]: ...
6260
def starmap_async(self,
63-
func: Callable[..., Any],
61+
func: Callable[..., _T],
6462
iterable: Iterable[Iterable[Any]] = ...,
6563
chunksize: Optional[int] = ...,
66-
callback: Optional[Callable[..., None]] = ...,
67-
error_callback: Optional[Callable[[BaseException], None]] = ...) -> AsyncResult: ...
64+
callback: Optional[Callable[[_T], None]] = ...,
65+
error_callback: Optional[Callable[[BaseException], None]] = ...) -> AsyncResult[List[_T]]: ...
6866
def close(self) -> None: ...
6967
def terminate(self) -> None: ...
7068
def join(self) -> None: ...
71-
def __enter__(self: _T) -> _T: ...
69+
def __enter__(self: _PT) -> _PT: ...
7270

7371

7472
class ThreadPool(Pool, ContextManager[ThreadPool]):

0 commit comments

Comments
 (0)