From ae8116cfa944dccad13638f6875b33b98d285b63 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 11 Nov 2023 02:23:27 +0300 Subject: gh-107431: Make `multiprocessing.managers.{DictProxy,ListProxy}` generic (#107433) Make `multiprocessing.managers.{DictProxy,ListProxy}` generic for type annotation use. `ListProxy[str]` for example. Co-authored-by: Alex Waygood Co-authored-by: Gregory P. Smith --- Lib/multiprocessing/managers.py | 8 ++++++-- Lib/test/test_genericalias.py | 6 ++++-- .../next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst | 2 ++ 3 files changed, 12 insertions(+), 4 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 273c22a..96cebc6 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1165,15 +1165,19 @@ class ListProxy(BaseListProxy): 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' )) -DictProxy._method_to_typeid_ = { +_BaseDictProxy._method_to_typeid_ = { '__iter__': 'Iterator', } +class DictProxy(_BaseDictProxy): + __class_getitem__ = classmethod(types.GenericAlias) ArrayProxy = MakeProxyType('ArrayProxy', ( diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index bf600a0..04cb810 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -28,7 +28,7 @@ from fileinput import FileInput 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 @@ try: 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 0000000..fb5bd8c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst @@ -0,0 +1,2 @@ +Make the ``DictProxy`` and ``ListProxy`` types in :mod:`multiprocessing.managers` +:ref:`Generic Alias Types` for ``[]`` use in typing contexts. -- cgit v0.12