From e467fd50ab181bad45eeef3a272a259d8e484290 Mon Sep 17 00:00:00 2001 From: Vishal Kuo Date: Fri, 26 Jun 2020 23:38:34 -0700 Subject: [PATCH] multiprocessing: add sharedctypes, fix some stubtest cases (#4282) --- stdlib/3/multiprocessing/__init__.pyi | 40 ++++++++------------- stdlib/3/multiprocessing/context.pyi | 7 ++-- stdlib/3/multiprocessing/sharedctypes.pyi | 42 +++++++++++++++++++++++ 3 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 stdlib/3/multiprocessing/sharedctypes.pyi diff --git a/stdlib/3/multiprocessing/__init__.pyi b/stdlib/3/multiprocessing/__init__.pyi index fb8021ffe6d8..f8d8f81a0e63 100644 --- a/stdlib/3/multiprocessing/__init__.pyi +++ b/stdlib/3/multiprocessing/__init__.pyi @@ -3,7 +3,7 @@ from typing import Any, Callable, Iterable, Optional, List, Union, Sequence, Tup from typing_extensions import Literal from ctypes import _CData from logging import Logger -from multiprocessing import connection, pool, spawn, synchronize +from multiprocessing import connection, pool, sharedctypes, spawn, synchronize from multiprocessing.context import ( AuthenticationError as AuthenticationError, BaseContext, @@ -26,7 +26,6 @@ if sys.version_info >= (3, 8): if sys.platform != "win32": from multiprocessing.context import ForkContext, ForkServerContext - # N.B. The functions below are generated at runtime by partially applying # multiprocessing.context.BaseContext's methods, so the two signatures should # be identical (modulo self). @@ -50,30 +49,21 @@ def Pool(processes: Optional[int] = ..., initargs: Iterable[Any] = ..., maxtasksperchild: Optional[int] = ...) -> pool.Pool: ... -class Array: - value: Any = ... - - def __init__(self, typecode_or_type: Union[str, Type[_CData]], size_or_initializer: Union[int, Sequence[Any]], *, lock: Union[bool, _LockLike] = ...) -> None: ... - def acquire(self) -> bool: ... - def release(self) -> bool: ... - def get_lock(self) -> _LockLike: ... - def get_obj(self) -> Any: ... - - @overload - def __getitem__(self, key: int) -> Any: ... - @overload - def __getitem__(self, key: slice) -> List[Any]: ... - def __getslice__(self, start: int, stop: int) -> Any: ... - def __setitem__(self, key: int, value: Any) -> None: ... - +# Functions Array and Value are copied from context.pyi. +# See https://github.com/python/typeshed/blob/ac234f25927634e06d9c96df98d72d54dd80dfc4/stdlib/2and3/turtle.pyi#L284-L291 +# for rationale +def Array( + typecode_or_type: Any, + size_or_initializer: Union[int, Sequence[Any]], + *, + lock: bool = ... +) -> sharedctypes._Array: ... -class Value(): - value: Any = ... - def __init__(self, typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ...) -> None: ... - def get_lock(self) -> _LockLike: ... - def get_obj(self) -> Any: ... - def acquire(self) -> bool: ... - def release(self) -> bool: ... +def Value( + typecode_or_type: Any, + *args: Any, + lock: bool = ... +) -> sharedctypes._Value: ... # ----- multiprocessing function stubs ----- def allow_connection_pickling() -> None: ... diff --git a/stdlib/3/multiprocessing/context.pyi b/stdlib/3/multiprocessing/context.pyi index ced1d26ebae2..9d391f922879 100644 --- a/stdlib/3/multiprocessing/context.pyi +++ b/stdlib/3/multiprocessing/context.pyi @@ -1,5 +1,6 @@ from logging import Logger import multiprocessing +from multiprocessing import sharedctypes from multiprocessing import synchronize from multiprocessing import queues from multiprocessing.process import BaseProcess @@ -73,23 +74,21 @@ class BaseContext(object): def RawArray(self, typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]]) -> Any: ... # TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out # how to handle the ctype - # TODO: change return to Value once a stub exists in multiprocessing.sharedctypes def Value( self, typecode_or_type: Any, *args: Any, lock: bool = ... - ) -> Any: ... + ) -> sharedctypes._Value: ... # TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out # how to handle the ctype - # TODO: change return to Array once a stub exists in multiprocessing.sharedctypes def Array( self, typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]], *, lock: bool = ... - ) -> Any: ... + ) -> sharedctypes._Array: ... def freeze_support(self) -> None: ... def get_logger(self) -> Logger: ... def log_to_stderr(self, level: Optional[str] = ...) -> Logger: ... diff --git a/stdlib/3/multiprocessing/sharedctypes.pyi b/stdlib/3/multiprocessing/sharedctypes.pyi new file mode 100644 index 000000000000..959cae5d12f2 --- /dev/null +++ b/stdlib/3/multiprocessing/sharedctypes.pyi @@ -0,0 +1,42 @@ +from ctypes import _CData +from typing import Any, List, Optional, Sequence, Type, Union, overload + +from multiprocessing.synchronize import _LockLike +from multiprocessing.context import BaseContext + +class _Array: + value: Any = ... + + def __init__(self, typecode_or_type: Union[str, Type[_CData]], size_or_initializer: Union[int, Sequence[Any]], *, lock: Union[bool, _LockLike] = ...) -> None: ... + def acquire(self) -> bool: ... + def release(self) -> bool: ... + def get_lock(self) -> _LockLike: ... + def get_obj(self) -> Any: ... + + @overload + def __getitem__(self, key: int) -> Any: ... + @overload + def __getitem__(self, key: slice) -> List[Any]: ... + def __getslice__(self, start: int, stop: int) -> Any: ... + def __setitem__(self, key: int, value: Any) -> None: ... + +class _Value(): + value: Any = ... + def __init__(self, typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ...) -> None: ... + def get_lock(self) -> _LockLike: ... + def get_obj(self) -> Any: ... + def acquire(self) -> bool: ... + def release(self) -> bool: ... + +def Array( + typecode_or_type: Union[str, Type[_CData]], + size_or_initializer: Union[int, Sequence[Any]], + *, + lock: Union[bool, _LockLike] = ..., + ctx: Optional[BaseContext] = ...) -> _Array: ... + +def Value( + typecode_or_type: Union[str, Type[_CData]], + *args: Any, + lock: Union[bool, _LockLike] = ..., + ctx: Optional[BaseContext] = ...) -> _Value: ...