Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8dbbbcd
gh-129250: allow pickle instances of generic classes
tom-pytel Jan 29, 2025
4173ce0
📜🤖 Added by blurb_it.
blurb-it[bot] Jan 29, 2025
dad90ca
PyLong_FromLong -> PyLong_FromSsize_t
tom-pytel Jan 29, 2025
6b2c84e
Merge branch 'main' into fix-issue-129250
tom-pytel Jan 29, 2025
f7cc7c6
remove weakref, owner stored as (mod, name, idx)
tom-pytel Jan 30, 2025
e794467
misc tweaks
tom-pytel Jan 30, 2025
1cdf1d7
set typevar owner explicitly one by one
tom-pytel Jan 31, 2025
46acd97
Merge branch 'main' into fix-issue-129250
tom-pytel Feb 6, 2025
0c2a469
LOAD_GLOBAL __name__ properly using nameop
tom-pytel Feb 6, 2025
88fa174
Merge branch 'main' into fix-issue-129250
tom-pytel Feb 12, 2025
c637ac8
Merge branch 'main' into fix-issue-129250
tom-pytel Feb 21, 2025
6465fda
Merge branch 'main' into fix-issue-129250
tom-pytel Feb 28, 2025
f143e98
requested misc changes
tom-pytel Mar 1, 2025
840023e
move _restore_anonymous_typeparam into _typing
tom-pytel Mar 1, 2025
66ac040
requested changes
tom-pytel Mar 1, 2025
0824e2f
requested changes
tom-pytel Mar 1, 2025
f407b3f
Merge branch 'main' into fix-issue-129250
tom-pytel Mar 19, 2025
df4822a
Merge branch 'main' into fix-issue-129250
tom-pytel Apr 4, 2025
db3ba3a
fix merge missing Py_ID() #include
tom-pytel Apr 4, 2025
ac2b1a6
Merge branch 'main' into fix-issue-129250
tom-pytel Apr 4, 2025
d1bf33c
Merge branch 'main' into fix-issue-129250
tom-pytel Apr 23, 2025
adeee1b
Merge branch 'main' into fix-issue-129250
tom-pytel Jun 10, 2025
e3ae1c4
Merge branch 'main' into fix-issue-129250
tom-pytel Aug 1, 2025
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
1 change: 1 addition & 0 deletions Include/internal/pycore_typevarobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern PyObject *_Py_set_typeparam_default(PyThreadState *, PyObject *, PyObject
extern int _Py_initialize_generic(PyInterpreterState *);
extern void _Py_clear_generic_types(PyInterpreterState *);
extern int _Py_typing_type_repr(PyUnicodeWriter *, PyObject *);
extern int _Py_set_type_params_owner_maybe(PyObject *, PyObject *);

extern PyTypeObject _PyTypeAlias_Type;
extern PyTypeObject _PyNoDefault_Type;
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,11 +1183,14 @@ def func1[X](x: X) -> X: ...
def func2[X, Y](x: X | Y) -> X | Y: ...
def func3[X, *Y, **Z](x: X, y: tuple[*Y], z: Z) -> X: ...
def func4[X: int, Y: (bytes, str)](x: X, y: Y) -> X | Y: ...
def func3b[X, *Y, **Z]() -> X: return Class3[X, Y, Z]()
def func5[Baz](): return Class5[Baz]()

class Class1[X]: ...
class Class2[X, Y]: ...
class Class3[X, *Y, **Z]: ...
class Class4[X: int, Y: (bytes, str)]: ...
class Class5(Generic[T]): pass


class TypeParamsPickleTest(unittest.TestCase):
Expand Down Expand Up @@ -1240,6 +1243,32 @@ def test_pickling_classes(self):
# but class check is good enough:
self.assertIsInstance(pickle.loads(pickled), real_class)

def test_pickling_anonymous_typeparams(self):
# see gh-129250
thing = func5()
pickled = pickle.dumps(thing)
unpickled = pickle.loads(pickled)
self.assertIs(unpickled.__orig_class__, thing.__orig_class__)
self.assertIs(unpickled.__orig_class__.__args__[0],
func5.__type_params__[0])

thing = func3b()
pickled = pickle.dumps(thing)
unpickled = pickle.loads(pickled)
self.assertIs(unpickled.__orig_class__, thing.__orig_class__)
self.assertIs(unpickled.__orig_class__.__args__[0],
func3b.__type_params__[0])
self.assertIs(unpickled.__orig_class__.__args__[1],
func3b.__type_params__[1])
self.assertIs(unpickled.__orig_class__.__args__[2],
func3b.__type_params__[2])

for i in range(3):
thing = Class3.__type_params__[i]
pickled = pickle.dumps(thing)
unpickled = pickle.loads(pickled)
self.assertIs(unpickled, thing)


class TypeParamsWeakRefTest(unittest.TestCase):
def test_weakrefs(self):
Expand Down
4 changes: 4 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
return t


def _restore_anonymous_typeparam(owner, index):
return owner.__type_params__[index]


class _Final:
"""Mixin to prohibit subclassing."""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow pickle of instances of generic classes.
12 changes: 7 additions & 5 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
/* Function object implementation */

#include "Python.h"
#include "pycore_dict.h" // _Py_INCREF_DICT()
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_dict.h" // _Py_INCREF_DICT()
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_typevarobject.h" // _Py_set_type_params_owner_maybe()


static const char *
Expand Down Expand Up @@ -926,6 +927,7 @@ _Py_set_function_type_params(PyThreadState *Py_UNUSED(ignored), PyObject *func,
assert(PyFunction_Check(func));
assert(PyTuple_Check(type_params));
PyFunctionObject *f = (PyFunctionObject *)func;
_Py_set_type_params_owner_maybe(type_params, func);
Py_XSETREF(f->func_typeparams, Py_NewRef(type_params));
return Py_NewRef(func);
}
Expand Down
Loading
Loading