]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107431: Make `multiprocessing.managers.{DictProxy,ListProxy}` generic (#107433)
authorNikita Sobolev <mail@sobolevn.me>
Fri, 10 Nov 2023 23:23:27 +0000 (02:23 +0300)
committerGitHub <noreply@github.com>
Fri, 10 Nov 2023 23:23:27 +0000 (23:23 +0000)
Make `multiprocessing.managers.{DictProxy,ListProxy}` generic for type annotation use.  `ListProxy[str]` for example.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/multiprocessing/managers.py
Lib/test/test_genericalias.py
Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst [new file with mode: 0644]

index 273c22a7654f05315794fa90cf3b25d8bee61f95..96cebc6eabec89e84cf69175418b5d29a82fd694 100644 (file)
@@ -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', (
index bf600a0f4d1834537652882c02074937f94cfd1f..04cb810d9babbf3baadc00bbd53e815bf687edbc 100644 (file)
@@ -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 (file)
index 0000000..fb5bd8c
--- /dev/null
@@ -0,0 +1,2 @@
+Make the ``DictProxy`` and ``ListProxy`` types in :mod:`multiprocessing.managers`
+:ref:`Generic Alias Types<types-genericalias>` for ``[]`` use in typing contexts.