Skip to content

Commit 71f020d

Browse files
authored
add annotation for multiprocessing.{Value,Array} with special c-types (#11833)
1 parent 273e9ea commit 71f020d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Diff for: stdlib/multiprocessing/context.pyi

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import ctypes
22
import sys
33
from collections.abc import Callable, Iterable, Sequence
4-
from ctypes import _CData
4+
from ctypes import _CData, _SimpleCData, c_char
55
from logging import Logger, _Level as _LoggingLevel
66
from multiprocessing import popen_fork, popen_forkserver, popen_spawn_posix, popen_spawn_win32, queues, synchronize
77
from multiprocessing.managers import SyncManager
88
from multiprocessing.pool import Pool as _Pool
99
from multiprocessing.process import BaseProcess
10-
from multiprocessing.sharedctypes import Synchronized, SynchronizedArray
10+
from multiprocessing.sharedctypes import Synchronized, SynchronizedArray, SynchronizedString
1111
from typing import Any, ClassVar, Literal, TypeVar, overload
1212
from typing_extensions import TypeAlias
1313

@@ -19,6 +19,7 @@ else:
1919
__all__ = ()
2020

2121
_LockLike: TypeAlias = synchronize.Lock | synchronize.RLock
22+
_T = TypeVar("_T")
2223
_CT = TypeVar("_CT", bound=_CData)
2324

2425
class ProcessError(Exception): ...
@@ -79,6 +80,10 @@ class BaseContext:
7980
@overload
8081
def RawArray(self, typecode_or_type: str, size_or_initializer: int | Sequence[Any]) -> Any: ...
8182
@overload
83+
def Value(
84+
self, typecode_or_type: type[_SimpleCData[_T]], *args: Any, lock: Literal[True] | _LockLike = True
85+
) -> Synchronized[_T]: ...
86+
@overload
8287
def Value(self, typecode_or_type: type[_CT], *args: Any, lock: Literal[False]) -> Synchronized[_CT]: ...
8388
@overload
8489
def Value(self, typecode_or_type: type[_CT], *args: Any, lock: Literal[True] | _LockLike = True) -> Synchronized[_CT]: ...
@@ -87,6 +92,10 @@ class BaseContext:
8792
@overload
8893
def Value(self, typecode_or_type: str | type[_CData], *args: Any, lock: bool | _LockLike = True) -> Any: ...
8994
@overload
95+
def Array(
96+
self, typecode_or_type: type[c_char], size_or_initializer: int | Sequence[Any], *, lock: Literal[True] | _LockLike = True
97+
) -> SynchronizedString: ...
98+
@overload
9099
def Array(
91100
self, typecode_or_type: type[_CT], size_or_initializer: int | Sequence[Any], *, lock: Literal[False]
92101
) -> SynchronizedArray[_CT]: ...

Diff for: test_cases/stdlib/check_multiprocessing.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from __future__ import annotations
2+
3+
from ctypes import c_char, c_float
4+
from multiprocessing import Array, Value
5+
from multiprocessing.sharedctypes import Synchronized, SynchronizedString
6+
from typing_extensions import assert_type
7+
8+
string = Array(c_char, 12)
9+
assert_type(string, SynchronizedString)
10+
assert_type(string.value, bytes)
11+
12+
field = Value(c_float, 0.0)
13+
assert_type(field, Synchronized[float])
14+
field.value = 1.2

0 commit comments

Comments
 (0)