Skip to content

Commit 8ef8750

Browse files
authored
bpo-39481: Make weakref and WeakSet generic (GH-19497)
1 parent cecf049 commit 8ef8750

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

‎Lib/_weakrefset.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# by abc.py to load everything else at startup.
44

55
from _weakref import ref
6+
from types import GenericAlias
67

78
__all__ = ['WeakSet']
89

@@ -197,3 +198,5 @@ def isdisjoint(self, other):
197198

198199
def __repr__(self):
199200
return repr(self.data)
201+
202+
__class_getitem__ = classmethod(GenericAlias)

‎Lib/test/test_genericalias.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from urllib.parse import SplitResult, ParseResult
3434
from unittest.case import _AssertRaisesContext
3535
from queue import Queue, SimpleQueue
36+
from weakref import WeakSet, ReferenceType, ref
3637
import typing
3738

3839
from typing import TypeVar
@@ -73,6 +74,7 @@ def test_subscriptable(self):
7374
Array, LibraryLoader,
7475
SplitResult, ParseResult,
7576
ValueProxy, ApplyResult,
77+
WeakSet, ReferenceType, ref,
7678
ShareableList, SimpleQueue,
7779
Future, _WorkItem,
7880
Morsel,

‎Objects/weakrefobject.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ static PyMemberDef weakref_members[] = {
362362
{NULL} /* Sentinel */
363363
};
364364

365+
static PyMethodDef weakref_methods[] = {
366+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
367+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
368+
{NULL} /* Sentinel */
369+
};
370+
365371
PyTypeObject
366372
_PyWeakref_RefType = {
367373
PyVarObject_HEAD_INIT(&PyType_Type, 0)
@@ -392,7 +398,7 @@ _PyWeakref_RefType = {
392398
0, /*tp_weaklistoffset*/
393399
0, /*tp_iter*/
394400
0, /*tp_iternext*/
395-
0, /*tp_methods*/
401+
weakref_methods, /*tp_methods*/
396402
weakref_members, /*tp_members*/
397403
0, /*tp_getset*/
398404
0, /*tp_base*/

0 commit comments

Comments
 (0)