From f2a17be9e66439a0f5d6489c4e4774064560a6ed Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Wed, 27 Jul 2022 16:49:07 -0400 Subject: [PATCH 1/6] feat: add support for resource-sharer type hints --- stdlib/multiprocessing/resource_sharer.pyi | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 stdlib/multiprocessing/resource_sharer.pyi diff --git a/stdlib/multiprocessing/resource_sharer.pyi b/stdlib/multiprocessing/resource_sharer.pyi new file mode 100644 index 000000000000..4d42855893b3 --- /dev/null +++ b/stdlib/multiprocessing/resource_sharer.pyi @@ -0,0 +1,51 @@ +from _typeshed import Incomplete +from collections.abc import Callable +import socket +import threading +import sys + +from .connection import Connection, Listener +from typing import Union +from typing_extensions import TypeAlias + +__all__ = ["stop"] + +# https://docs.python.org/3/library/multiprocessing.html#address-formats +_Address: TypeAlias = Union[str, tuple[str, int]] + +if sys.platform == "win32": + __all__ += ["DupSocket"] + + class DupSocket: + _id: tuple[_Address, int] + + def __init__(self, sock: socket.socket) -> None: ... + def detach(self) -> bytes: ... + +else: + __all__ += ["DupFd"] + + class DupFd: + _id: tuple[_Address, int] + + def __init__(self, fd: int) -> None: ... + def detach(self) -> None: ... + +class _ResourceSharer: + _key: int + _cache: dict[int, tuple[Incomplete, Incomplete]] + _lock: threading.Lock + _listener: Listener | None + _address: _Address | None + _thread: threading.Thread | None + def __init__(self) -> None: ... + def register(self, send: Callable[[Connection, int], None], close: Callable[[], None]) -> tuple[_Address, int]: ... + @staticmethod + def get_connection(ident: tuple[_Address, int]) -> Connection: ... + def stop(self, timeout: float | None = ...) -> None: ... + def _afterfork(self) -> None: ... + def _start(self) -> None: ... + def _serve(self) -> None: ... + +_resource_sharer: _ResourceSharer +stop = _resource_sharer.stop From df967d5d9093226659ec2c2256d4801fd168d2d1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Jul 2022 20:52:50 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/multiprocessing/resource_sharer.pyi | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/multiprocessing/resource_sharer.pyi b/stdlib/multiprocessing/resource_sharer.pyi index 4d42855893b3..5b644f146b99 100644 --- a/stdlib/multiprocessing/resource_sharer.pyi +++ b/stdlib/multiprocessing/resource_sharer.pyi @@ -1,13 +1,13 @@ -from _typeshed import Incomplete -from collections.abc import Callable import socket -import threading import sys - -from .connection import Connection, Listener +import threading +from _typeshed import Incomplete +from collections.abc import Callable from typing import Union from typing_extensions import TypeAlias +from .connection import Connection, Listener + __all__ = ["stop"] # https://docs.python.org/3/library/multiprocessing.html#address-formats From 706eb8f809a56250ba8e53dd9f8eaf63a3a23e99 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Wed, 27 Jul 2022 17:04:32 -0400 Subject: [PATCH 3/6] refactor: set default value for _resource_sharer due to method exposure Co-authored-by: Alex Waygood --- stdlib/multiprocessing/resource_sharer.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/multiprocessing/resource_sharer.pyi b/stdlib/multiprocessing/resource_sharer.pyi index 5b644f146b99..97905a62ebd5 100644 --- a/stdlib/multiprocessing/resource_sharer.pyi +++ b/stdlib/multiprocessing/resource_sharer.pyi @@ -47,5 +47,5 @@ class _ResourceSharer: def _start(self) -> None: ... def _serve(self) -> None: ... -_resource_sharer: _ResourceSharer +_resource_sharer: _ResourceSharer = ... stop = _resource_sharer.stop From c08e49e49db92f7b4400990b0739776dca8b2fa6 Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Wed, 27 Jul 2022 16:50:00 -0400 Subject: [PATCH 4/6] fix: remove private types --- stdlib/multiprocessing/resource_sharer.pyi | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/stdlib/multiprocessing/resource_sharer.pyi b/stdlib/multiprocessing/resource_sharer.pyi index 97905a62ebd5..13301c82f7b3 100644 --- a/stdlib/multiprocessing/resource_sharer.pyi +++ b/stdlib/multiprocessing/resource_sharer.pyi @@ -1,12 +1,11 @@ -import socket import sys -import threading -from _typeshed import Incomplete from collections.abc import Callable + +from socket import socket from typing import Union from typing_extensions import TypeAlias -from .connection import Connection, Listener +from .connection import Connection __all__ = ["stop"] @@ -17,35 +16,22 @@ if sys.platform == "win32": __all__ += ["DupSocket"] class DupSocket: - _id: tuple[_Address, int] - - def __init__(self, sock: socket.socket) -> None: ... + def __init__(self, sock: socket) -> None: ... def detach(self) -> bytes: ... else: __all__ += ["DupFd"] class DupFd: - _id: tuple[_Address, int] - def __init__(self, fd: int) -> None: ... def detach(self) -> None: ... class _ResourceSharer: - _key: int - _cache: dict[int, tuple[Incomplete, Incomplete]] - _lock: threading.Lock - _listener: Listener | None - _address: _Address | None - _thread: threading.Thread | None def __init__(self) -> None: ... def register(self, send: Callable[[Connection, int], None], close: Callable[[], None]) -> tuple[_Address, int]: ... @staticmethod def get_connection(ident: tuple[_Address, int]) -> Connection: ... def stop(self, timeout: float | None = ...) -> None: ... - def _afterfork(self) -> None: ... - def _start(self) -> None: ... - def _serve(self) -> None: ... _resource_sharer: _ResourceSharer = ... stop = _resource_sharer.stop From d2046c75d62f398c71b31ce0e36681434eed34da Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Jul 2022 21:41:51 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/multiprocessing/resource_sharer.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/multiprocessing/resource_sharer.pyi b/stdlib/multiprocessing/resource_sharer.pyi index 13301c82f7b3..a1c2f7025f7f 100644 --- a/stdlib/multiprocessing/resource_sharer.pyi +++ b/stdlib/multiprocessing/resource_sharer.pyi @@ -1,6 +1,5 @@ import sys from collections.abc import Callable - from socket import socket from typing import Union from typing_extensions import TypeAlias From c2675fd6f45922e3ab55b104d73610e063fd89fe Mon Sep 17 00:00:00 2001 From: Kevin Kirsche Date: Thu, 28 Jul 2022 10:57:14 -0400 Subject: [PATCH 6/6] fix: feedback RE: private and return types --- stdlib/multiprocessing/resource_sharer.pyi | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/stdlib/multiprocessing/resource_sharer.pyi b/stdlib/multiprocessing/resource_sharer.pyi index a1c2f7025f7f..b0d95514e7bd 100644 --- a/stdlib/multiprocessing/resource_sharer.pyi +++ b/stdlib/multiprocessing/resource_sharer.pyi @@ -1,11 +1,8 @@ import sys -from collections.abc import Callable from socket import socket from typing import Union from typing_extensions import TypeAlias -from .connection import Connection - __all__ = ["stop"] # https://docs.python.org/3/library/multiprocessing.html#address-formats @@ -16,21 +13,13 @@ if sys.platform == "win32": class DupSocket: def __init__(self, sock: socket) -> None: ... - def detach(self) -> bytes: ... + def detach(self) -> socket: ... else: __all__ += ["DupFd"] class DupFd: def __init__(self, fd: int) -> None: ... - def detach(self) -> None: ... - -class _ResourceSharer: - def __init__(self) -> None: ... - def register(self, send: Callable[[Connection, int], None], close: Callable[[], None]) -> tuple[_Address, int]: ... - @staticmethod - def get_connection(ident: tuple[_Address, int]) -> Connection: ... - def stop(self, timeout: float | None = ...) -> None: ... + def detach(self) -> int: ... -_resource_sharer: _ResourceSharer = ... -stop = _resource_sharer.stop +def stop(timeout: float | None = ...) -> None: ...