Skip to content

Commit

Permalink
gh-107431: Make multiprocessing.managers.{DictProxy,ListProxy} gene…
Browse files Browse the repository at this point in the history
…ric (#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>
  • Loading branch information
3 people committed Nov 10, 2023
1 parent 06c47a3 commit ae8116c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,15 +1165,19 @@ 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'
))
DictProxy._method_to_typeid_ = {
_BaseDictProxy._method_to_typeid_ = {
'__iter__': 'Iterator',
}
class DictProxy(_BaseDictProxy):
__class_getitem__ = classmethod(types.GenericAlias)


ArrayProxy = MakeProxyType('ArrayProxy', (
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
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
from multiprocessing.queues import JoinableQueue as MPJoinableQueue
except ImportError:
# _multiprocessing module is optional
ValueProxy = None
DictProxy = None
ListProxy = None
ApplyResult = None
MPSimpleQueue = None
MPQueue = None
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ae8116c

Please sign in to comment.