Skip to content

gh-106403: Restore weakref support for TypeVar and friends #106418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import types
import unittest
import pickle
import weakref
from test.support import requires_working_socket, check_syntax_error, run_code

from typing import Generic, Sequence, TypeVar, TypeVarTuple, ParamSpec, get_args
Expand Down Expand Up @@ -921,3 +922,33 @@ def test_pickling_classes(self):
# These instances are not equal,
# but class check is good enough:
self.assertIsInstance(pickle.loads(pickled), real_class)


class TypeParamsWeakRefTest(unittest.TestCase):
def test_weakrefs(self):
T = TypeVar('T')
P = ParamSpec('P')
class OldStyle(Generic[T]):
pass

class NewStyle[T]:
pass

cases = [
T,
TypeVar('T', bound=int),
P,
P.args,
P.kwargs,
TypeVarTuple('Ts'),
OldStyle,
OldStyle[int],
OldStyle(),
NewStyle,
NewStyle[int],
NewStyle(),
Generic[T],
]
for case in cases:
with self.subTest(case=case):
weakref.ref(case)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Instances of :class:`typing.TypeVar`, :class:`typing.ParamSpec`,
:class:`typing.ParamSpecArgs`, :class:`typing.ParamSpecKwargs`, and
:class:`typing.TypeVarTuple` once again support weak references, fixing a
regression introduced in Python 3.12.0 beta 1. Patch by Jelle Zijlstra.
12 changes: 7 additions & 5 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ PyType_Spec typevar_spec = {
.name = "typing.TypeVar",
.basicsize = sizeof(typevarobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_MANAGED_DICT,
| Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_MANAGED_WEAKREF,
.slots = typevar_slots,
};

Expand Down Expand Up @@ -647,7 +647,8 @@ static PyType_Slot paramspecargs_slots[] = {
PyType_Spec paramspecargs_spec = {
.name = "typing.ParamSpecArgs",
.basicsize = sizeof(paramspecattrobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_MANAGED_WEAKREF,
.slots = paramspecargs_slots,
};

Expand Down Expand Up @@ -726,7 +727,8 @@ static PyType_Slot paramspeckwargs_slots[] = {
PyType_Spec paramspeckwargs_spec = {
.name = "typing.ParamSpecKwargs",
.basicsize = sizeof(paramspecattrobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_MANAGED_WEAKREF,
.slots = paramspeckwargs_slots,
};

Expand Down Expand Up @@ -1007,7 +1009,7 @@ PyType_Spec paramspec_spec = {
.name = "typing.ParamSpec",
.basicsize = sizeof(paramspecobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_MANAGED_DICT,
| Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_MANAGED_WEAKREF,
.slots = paramspec_slots,
};

Expand Down Expand Up @@ -1228,7 +1230,7 @@ PyType_Spec typevartuple_spec = {
.name = "typing.TypeVarTuple",
.basicsize = sizeof(typevartupleobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_DICT
| Py_TPFLAGS_HAVE_GC,
| Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MANAGED_WEAKREF,
.slots = typevartuple_slots,
};

Expand Down