From 3f865c58ba5ffc97cfd0a2619f54a6deb749d79a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 29 Jul 2023 19:01:16 +0300 Subject: [PATCH 1/5] gh-107431: Make `multiprocessing.managers.{DictProxy,ListProxy}` generic --- Lib/multiprocessing/managers.py | 13 +++++++++---- Lib/test/test_genericalias.py | 6 ++++-- .../2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index b6534939b4d98b..45593a959052a7 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1159,16 +1159,21 @@ def __imul__(self, value): self._callmethod('__imul__', (value,)) return self + __class_getitem__ = classmethod(types.GenericAlias) + -DictProxy = MakeProxyType('DictProxy', ( +BaseDictProxy = MakeProxyType('DictProxy', ( '__contains__', '__delitem__', '__getitem__', '__iter__', '__len__', '__setitem__', 'clear', 'copy', 'get', 'items', - 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values' + 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', )) -DictProxy._method_to_typeid_ = { - '__iter__': 'Iterator', +class DictProxy(BaseDictProxy): + _method_to_typeid_ = { + '__iter__': 'Iterator', } + __class_getitem__ = classmethod(types.GenericAlias) + ArrayProxy = MakeProxyType('ArrayProxy', ( '__len__', '__getitem__', '__setitem__' diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index bf600a0f4d1834..04cb810d9babbf 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -28,7 +28,7 @@ from itertools import chain from http.cookies import Morsel try: - from multiprocessing.managers import ValueProxy + from multiprocessing.managers import ValueProxy, DictProxy, ListProxy from multiprocessing.pool import ApplyResult from multiprocessing.queues import SimpleQueue as MPSimpleQueue from multiprocessing.queues import Queue as MPQueue @@ -36,6 +36,8 @@ except ImportError: # _multiprocessing module is optional ValueProxy = None + DictProxy = None + ListProxy = None ApplyResult = None MPSimpleQueue = None MPQueue = None @@ -134,7 +136,7 @@ class BaseTest(unittest.TestCase): if ctypes is not None: generic_types.extend((ctypes.Array, ctypes.LibraryLoader)) if ValueProxy is not None: - generic_types.extend((ValueProxy, ApplyResult, + generic_types.extend((ValueProxy, DictProxy, ListProxy, ApplyResult, MPSimpleQueue, MPQueue, MPJoinableQueue)) def test_subscriptable(self): diff --git a/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst b/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst new file mode 100644 index 00000000000000..b54a436ff544d1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst @@ -0,0 +1,2 @@ +Make ``DictProxy`` and ``ListProxy`` types in ``multiprocessing.managers`` +generic. From 179e17f098dfc6454f5814a93cc74d1e8a6e081a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 29 Jul 2023 19:17:43 +0300 Subject: [PATCH 2/5] Address review --- Lib/multiprocessing/managers.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index 45593a959052a7..19f3d27610018a 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1165,13 +1165,12 @@ def __imul__(self, value): BaseDictProxy = MakeProxyType('DictProxy', ( '__contains__', '__delitem__', '__getitem__', '__iter__', '__len__', '__setitem__', 'clear', 'copy', 'get', 'items', - 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', + 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values' )) +BaseDictProxy._method_to_typeid_ = { + '__iter__': 'Iterator', +} class DictProxy(BaseDictProxy): - _method_to_typeid_ = { - '__iter__': 'Iterator', - } - __class_getitem__ = classmethod(types.GenericAlias) From 831530c87f7c7024beaee76bbb7bda93f4c07709 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 29 Jul 2023 17:19:48 +0100 Subject: [PATCH 3/5] further minimise diff --- Lib/multiprocessing/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index 19f3d27610018a..cd61788fabf52b 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1169,7 +1169,7 @@ def __imul__(self, value): )) BaseDictProxy._method_to_typeid_ = { '__iter__': 'Iterator', -} + } class DictProxy(BaseDictProxy): __class_getitem__ = classmethod(types.GenericAlias) From dd92d1bff786ffa586f937045bfce96f9dd612b4 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Fri, 10 Nov 2023 13:55:00 -0800 Subject: [PATCH 4/5] s/BaseDictProxy/_BaseDictProxy/ --- Lib/multiprocessing/managers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index cd61788fabf52b..42f5e8565301d2 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1162,15 +1162,15 @@ def __imul__(self, value): __class_getitem__ = classmethod(types.GenericAlias) -BaseDictProxy = MakeProxyType('DictProxy', ( +_BaseDictProxy = MakeProxyType('DictProxy', ( '__contains__', '__delitem__', '__getitem__', '__iter__', '__len__', '__setitem__', 'clear', 'copy', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values' )) -BaseDictProxy._method_to_typeid_ = { +_BaseDictProxy._method_to_typeid_ = { '__iter__': 'Iterator', } -class DictProxy(BaseDictProxy): +class DictProxy(_BaseDictProxy): __class_getitem__ = classmethod(types.GenericAlias) From 71b827ab1a09c31d0d0c8d7ed94632373f17bc81 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Fri, 10 Nov 2023 13:59:50 -0800 Subject: [PATCH 5/5] More ReST crosslinks and explanation in NEWS. --- .../Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst b/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst index b54a436ff544d1..fb5bd8cb7cad47 100644 --- a/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst +++ b/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst @@ -1,2 +1,2 @@ -Make ``DictProxy`` and ``ListProxy`` types in ``multiprocessing.managers`` -generic. +Make the ``DictProxy`` and ``ListProxy`` types in :mod:`multiprocessing.managers` +:ref:`Generic Alias Types` for ``[]`` use in typing contexts.