summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-11-10 23:23:27 (GMT)
committerGitHub <noreply@github.com>2023-11-10 23:23:27 (GMT)
commitae8116cfa944dccad13638f6875b33b98d285b63 (patch)
treed7ef2787e60d7ae2102cae181627d2cc60e0075b
parent06c47a305d8f7f4f56a1113d9eb2eddcc175f2ed (diff)
downloadcpython-ae8116cfa944dccad13638f6875b33b98d285b63.zip
cpython-ae8116cfa944dccad13638f6875b33b98d285b63.tar.gz
cpython-ae8116cfa944dccad13638f6875b33b98d285b63.tar.bz2
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 <Alex.Waygood@Gmail.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
-rw-r--r--Lib/multiprocessing/managers.py8
-rw-r--r--Lib/test/test_genericalias.py6
-rw-r--r--Misc/NEWS.d/next/Library/2023-07-29-19-00-39.gh-issue-107431.1GzJ2p.rst2
3 files changed, 12 insertions, 4 deletions
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<types-genericalias>` for ``[]`` use in typing contexts.