Make the return type of multiprocessing.connection.Pipe more precise (#8706)

The precise return type depends on the platform. Link to implementation:
68fb03249f/Lib/multiprocessing/connection.py (L518)

Now users no longer need to use the internal-looking class
`_ConnectionBase` in annotations, at least in code that doesn't
need to be cross-platform.
This commit is contained in:
Jukka Lehtosalo
2022-09-08 13:24:49 +01:00
committed by GitHub
parent fdbf1396d1
commit e4d0d3d9d7
2 changed files with 23 additions and 3 deletions

View File

@@ -58,4 +58,12 @@ def wait(
object_list: Iterable[Connection | socket.socket | int], timeout: float | None = ...
) -> list[Connection | socket.socket | int]: ...
def Client(address: _Address, family: str | None = ..., authkey: bytes | None = ...) -> Connection: ...
def Pipe(duplex: bool = ...) -> tuple[_ConnectionBase, _ConnectionBase]: ...
# N.B. Keep this in sync with multiprocessing.context.BaseContext.Pipe.
# _ConnectionBase is the common base class of Connection and PipeConnection
# and can be used in cross-platform code.
if sys.platform != "win32":
def Pipe(duplex: bool = ...) -> tuple[Connection, Connection]: ...
else:
def Pipe(duplex: bool = ...) -> tuple[PipeConnection, PipeConnection]: ...

View File

@@ -4,7 +4,6 @@ from collections.abc import Callable, Iterable, Sequence
from ctypes import _CData
from logging import Logger
from multiprocessing import popen_fork, popen_forkserver, popen_spawn_posix, popen_spawn_win32, queues, synchronize
from multiprocessing.connection import _ConnectionBase
from multiprocessing.managers import SyncManager
from multiprocessing.pool import Pool as _Pool
from multiprocessing.process import BaseProcess
@@ -12,6 +11,11 @@ from multiprocessing.sharedctypes import SynchronizedArray, SynchronizedBase
from typing import Any, ClassVar, TypeVar, overload
from typing_extensions import Literal, TypeAlias
if sys.platform != "win32":
from multiprocessing.connection import Connection
else:
from multiprocessing.connection import PipeConnection
if sys.version_info >= (3, 8):
__all__ = ()
else:
@@ -43,7 +47,15 @@ class BaseContext:
def active_children() -> list[BaseProcess]: ...
def cpu_count(self) -> int: ...
def Manager(self) -> SyncManager: ...
def Pipe(self, duplex: bool = ...) -> tuple[_ConnectionBase, _ConnectionBase]: ...
# N.B. Keep this in sync with multiprocessing.connection.Pipe.
# _ConnectionBase is the common base class of Connection and PipeConnection
# and can be used in cross-platform code.
if sys.platform != "win32":
def Pipe(self, duplex: bool = ...) -> tuple[Connection, Connection]: ...
else:
def Pipe(self, duplex: bool = ...) -> tuple[PipeConnection, PipeConnection]: ...
def Barrier(
self, parties: int, action: Callable[..., object] | None = ..., timeout: float | None = ...
) -> synchronize.Barrier: ...