Skip to content

Commit a7302dc

Browse files
authored
Add aliases for 'function classes' to multiprocessing (#5346)
Part of #4313
1 parent 4166a41 commit a7302dc

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

stdlib/multiprocessing/__init__.pyi

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ from multiprocessing.context import (
1515
from multiprocessing.managers import SyncManager
1616
from multiprocessing.process import active_children as active_children, current_process as current_process
1717

18-
# These are technically functions that return instances of these Queue classes. See #4313 for discussion
18+
# These are technically functions that return instances of these Queue classes.
19+
# Using them as annotations is deprecated. Either use imports from
20+
# multiprocessing.queues or the aliases defined below. See #4266 for discussion.
1921
from multiprocessing.queues import JoinableQueue as JoinableQueue, Queue as Queue, SimpleQueue as SimpleQueue
2022
from multiprocessing.spawn import freeze_support as freeze_support
2123
from typing import Any, Optional, Union, overload
@@ -27,6 +29,26 @@ if sys.version_info >= (3, 8):
2729
if sys.platform != "win32":
2830
from multiprocessing.context import ForkContext, ForkServerContext
2931

32+
# The following type aliases can be used to annotate the return values of
33+
# the corresponding functions. They are not defined at runtime.
34+
#
35+
# from multiprocessing import Lock
36+
# from typing import TYPE_CHECKING
37+
# if TYPE_CHECKING:
38+
# from multiprocessing import _LockType
39+
# lock: _LockType = Lock()
40+
41+
_QueueType = Queue
42+
_SimpleQueueType = SimpleQueue
43+
_JoinableQueueType = JoinableQueue
44+
_BarrierType = synchronize.Barrier
45+
_BoundedSemaphoreType = synchronize.BoundedSemaphore
46+
_ConditionType = synchronize.Condition
47+
_EventType = synchronize.Event
48+
_LockType = synchronize.Lock
49+
_RLockType = synchronize.RLock
50+
_SemaphoreType = synchronize.Semaphore
51+
3052
# N.B. The functions below are generated at runtime by partially applying
3153
# multiprocessing.context.BaseContext's methods, so the two signatures should
3254
# be identical (modulo self).
@@ -38,13 +60,13 @@ RawArray = context._default_context.RawArray
3860
Value = context._default_context.Value
3961
Array = context._default_context.Array
4062

41-
def Barrier(parties: int, action: Optional[Callable[..., Any]] = ..., timeout: Optional[float] = ...) -> synchronize.Barrier: ...
42-
def BoundedSemaphore(value: int = ...) -> synchronize.BoundedSemaphore: ...
43-
def Condition(lock: Optional[_LockLike] = ...) -> synchronize.Condition: ...
44-
def Event() -> synchronize.Event: ...
45-
def Lock() -> synchronize.Lock: ...
46-
def RLock() -> synchronize.RLock: ...
47-
def Semaphore(value: int = ...) -> synchronize.Semaphore: ...
63+
def Barrier(parties: int, action: Optional[Callable[..., Any]] = ..., timeout: Optional[float] = ...) -> _BarrierType: ...
64+
def BoundedSemaphore(value: int = ...) -> _BoundedSemaphoreType: ...
65+
def Condition(lock: Optional[_LockLike] = ...) -> _ConditionType: ...
66+
def Event() -> _EventType: ...
67+
def Lock() -> _LockType: ...
68+
def RLock() -> _RLockType: ...
69+
def Semaphore(value: int = ...) -> _SemaphoreType: ...
4870
def Pipe(duplex: bool = ...) -> tuple[connection.Connection, connection.Connection]: ...
4971
def Pool(
5072
processes: Optional[int] = ...,

0 commit comments

Comments
 (0)