Skip to content

Commit 070bbb4

Browse files
authored
Add multiprocessing.reduction submodule (#7361)
1 parent 1610949 commit 070bbb4

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"**/@python2",
1010
"stdlib/distutils/command",
1111
"stdlib/lib2to3/refactor.pyi",
12+
"stdlib/multiprocessing/reduction.pyi",
1213
"stdlib/sqlite3/dbapi2.pyi",
1314
"stdlib/_tkinter.pyi",
1415
"stdlib/tkinter",

stdlib/copyreg.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ def add_extension(module: Hashable, name: Hashable, code: SupportsInt) -> None:
1515
def remove_extension(module: Hashable, name: Hashable, code: int) -> None: ...
1616
def clear_extension_cache() -> None: ...
1717

18-
dispatch_table: dict[type, Callable[[type], str | _Reduce[type]]] # undocumented
18+
_DispatchTableType = dict[type, Callable[[type], str | _Reduce[type]]] # imported by multiprocessing.reduction
19+
dispatch_table: _DispatchTableType # undocumented

stdlib/multiprocessing/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from collections.abc import Callable, Iterable
33
from logging import Logger
4-
from multiprocessing import connection, context, pool, synchronize
4+
from multiprocessing import connection, context, pool, reduction as reducer, synchronize
55
from multiprocessing.context import (
66
AuthenticationError as AuthenticationError,
77
BaseContext,

stdlib/multiprocessing/reduction.pyi

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import pickle
2+
import sys
3+
from abc import ABCMeta
4+
from copyreg import _DispatchTableType
5+
from typing import Any
6+
from typing_extensions import Literal
7+
8+
if sys.platform == "win32":
9+
__all__ = ["send_handle", "recv_handle", "ForkingPickler", "register", "dump", "DupHandle", "duplicate", "steal_handle"]
10+
else:
11+
__all__ = ["send_handle", "recv_handle", "ForkingPickler", "register", "dump", "DupFd", "sendfds", "recvfds"]
12+
13+
class ForkingPickler(pickle.Pickler):
14+
dispatch_table: _DispatchTableType
15+
def __init__(self, *args) -> None: ...
16+
@classmethod
17+
def register(cls, type, reduce) -> None: ...
18+
@classmethod
19+
def dumps(cls, obj, protocol: Any | None = ...): ...
20+
loads = pickle.loads
21+
22+
register = ForkingPickler.register
23+
24+
def dump(obj, file, protocol: Any | None = ...) -> None: ...
25+
26+
if sys.platform == "win32":
27+
if sys.version_info >= (3, 8):
28+
def duplicate(handle, target_process: Any | None = ..., inheritable: bool = ..., *, source_process: Any | None = ...): ...
29+
else:
30+
def duplicate(handle, target_process: Any | None = ..., inheritable: bool = ...): ...
31+
32+
def steal_handle(source_pid, handle): ...
33+
def send_handle(conn, handle, destination_pid) -> None: ...
34+
def recv_handle(conn): ...
35+
36+
class DupHandle:
37+
def __init__(self, handle, access, pid: Any | None = ...) -> None: ...
38+
def detach(self): ...
39+
40+
else:
41+
if sys.platform == "darwin":
42+
ACKNOWLEDGE: Literal[True]
43+
else:
44+
ACKNOWLEDGE: Literal[False]
45+
46+
def recvfds(sock, size): ...
47+
def send_handle(conn, handle, destination_pid) -> None: ...
48+
def recv_handle(conn) -> None: ...
49+
def sendfds(sock, fds) -> None: ...
50+
def DupFd(fd): ...
51+
52+
# These aliases are to work around pyright complaints.
53+
# Pyright doesn't like it when a class object is defined as an alias
54+
# of a global object with the same name.
55+
_ForkingPickler = ForkingPickler
56+
_register = register
57+
_dump = dump
58+
_send_handle = send_handle
59+
_recv_handle = recv_handle
60+
61+
if sys.platform == "win32":
62+
_steal_handle = steal_handle
63+
_duplicate = duplicate
64+
_DupHandle = DupHandle
65+
else:
66+
_sendfds = sendfds
67+
_recvfds = recvfds
68+
_DupFd = DupFd
69+
70+
class AbstractReducer(metaclass=ABCMeta):
71+
ForkingPickler = _ForkingPickler
72+
register = _register
73+
dump = _dump
74+
send_handle = _send_handle
75+
recv_handle = _recv_handle
76+
if sys.platform == "win32":
77+
steal_handle = _steal_handle
78+
duplicate = _duplicate
79+
DupHandle = _DupHandle
80+
else:
81+
sendfds = _sendfds
82+
recvfds = _recvfds
83+
DupFd = _DupFd
84+
def __init__(self, *args) -> None: ...

tests/stubtest_allowlists/py3_common.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ threading.Lock # A factory function that returns 'most efficient lock'. Marking
200200
threading.RLock # Similar to above
201201
multiprocessing.dummy.Lock # Similar to above
202202
multiprocessing.dummy.RLock # Similar to above
203+
# alias for a class defined elsewhere, mypy infers the variable has type `(*args) -> ForkingPickler` but stubtest infers the runtime type as <class ForkingPickler>
204+
multiprocessing.reduction.AbstractReducer.ForkingPickler
203205
tkinter.Misc.grid_propagate # The noarg placeholder is a set value list
204206
tkinter.Misc.pack_propagate # The noarg placeholder is a set value list
205207
tkinter.Tk.eval # from __getattr__
@@ -699,7 +701,6 @@ multiprocessing.managers.SyncManager.JoinableQueue
699701
multiprocessing.managers.SyncManager.Pool
700702
multiprocessing.pool.Pool.Process
701703
multiprocessing.pool.ThreadPool.Process
702-
multiprocessing.reducer
703704
multiprocessing.synchronize.Semaphore.get_value
704705
rlcompleter.Completer.attr_matches
705706
rlcompleter.Completer.global_matches

tests/stubtest_allowlists/win32.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ distutils.msvccompiler.HKEYS
77
locale.[A-Z0-9_]+ # Constants that should be moved to _locale and re-exported conditionally
88
locale.nl_langinfo # Function that should be moved to _locale and re-exported conditionally
99
mmap.PAGESIZE
10+
# alias for a class defined elsewhere,
11+
# mypy infers the variable has type `(*args) -> DupHandle` but stubtest infers the runtime type as <class DupHandle>
12+
multiprocessing.reduction.AbstractReducer.DupHandle
1013
msilib.MSI[A-Z_]+
1114
msilib.text.dirname
1215
msilib.PID_[A-Z_]+

0 commit comments

Comments
 (0)