Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-129250: allow pickle instances of generic classes #129446

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 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
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_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ int _PyCompile_ScopeType(struct _PyCompiler *c);
int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
int _PyCompile_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
PyObject *_PyCompile_Qualname(struct _PyCompiler *c);
PyObject *_PyCompile_PeekQualname(struct _PyCompiler *c, PyObject *name);
_PyCompile_CodeUnitMetadata *_PyCompile_Metadata(struct _PyCompiler *c);
PyObject *_PyCompile_StaticAttributesAsTuple(struct _PyCompiler *c);

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void _PyFunction_ClearCodeByVersion(uint32_t version);
PyFunctionObject *_PyFunction_LookupByVersion(uint32_t version, PyObject **p_code);

extern PyObject *_Py_set_function_type_params(
PyThreadState* unused, PyObject *func, PyObject *type_params);
PyThreadState *ts, PyObject *func, PyObject *type_params);


/* See pycore_code.h for explanation about what "stateless" means. */
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#define INTRINSIC_TYPEVAR_WITH_CONSTRAINTS 3
#define INTRINSIC_SET_FUNCTION_TYPE_PARAMS 4
#define INTRINSIC_SET_TYPEPARAM_DEFAULT 5
#define INTRINSIC_SET_TYPEPARAM_OWNER 6

#define MAX_INTRINSIC_2 5
#define MAX_INTRINSIC_2 6

typedef PyObject *(*intrinsic_func1)(PyThreadState* tstate, PyObject *value);
typedef PyObject *(*intrinsic_func2)(PyThreadState* tstate, PyObject *value1, PyObject *value2);
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_typevarobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern PyObject *_Py_make_typevartuple(PyThreadState *, PyObject *);
extern PyObject *_Py_make_typealias(PyThreadState *, PyObject *);
extern PyObject *_Py_subscript_generic(PyThreadState *, PyObject *);
extern PyObject *_Py_set_typeparam_default(PyThreadState *, PyObject *, PyObject *);
extern PyObject *_Py_set_typeparam_owner(PyThreadState *, PyObject *, PyObject *);
extern int _Py_initialize_generic(PyInterpreterState *);
extern void _Py_clear_generic_types(PyInterpreterState *);
extern int _Py_typing_type_repr(PyUnicodeWriter *, PyObject *);
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,11 +1183,15 @@ 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 Class1[Baz]()

class Class1[X]: ...
class Class2[X, Y]: ...
class Class3[X, *Y, **Z]: ...
class Class4[X: int, Y: (bytes, str)]: ...
class Class5:
def meth[Baz](): return Class1[Baz]()


class TypeParamsPickleTest(unittest.TestCase):
Expand Down Expand Up @@ -1240,6 +1244,39 @@ 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)

thing = Class5.meth()
pickled = pickle.dumps(thing)
unpickled = pickle.loads(pickled)
self.assertIs(unpickled.__orig_class__, thing.__orig_class__)
self.assertIs(unpickled.__orig_class__.__args__[0],
Class5.meth.__type_params__[0])


class TypeParamsWeakRefTest(unittest.TestCase):
def test_weakrefs(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix pickling of generic classes instances.
29 changes: 28 additions & 1 deletion Modules/_typingmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "internal/pycore_interp.h"
#include "internal/pycore_typevarobject.h"
#include "internal/pycore_unionobject.h" // _PyUnion_Type
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_runtime.h" // _Py_ID()
#include "clinic/_typingmodule.c.h"

/*[clinic input]
Expand All @@ -35,8 +36,34 @@ _typing__idfunc(PyObject *module, PyObject *x)
}


/*[clinic input]
_typing._restore_anonymous_typeparam -> object

owner: object
index: object
/

Restore previously pickled anonymous type param from object.__type_params__.
[clinic start generated code]*/

static PyObject *
_typing__restore_anonymous_typeparam_impl(PyObject *module, PyObject *owner,
PyObject *index)
/*[clinic end generated code: output=00baec27dbf8d2d9 input=2f048db28d8124fb]*/
{
PyObject *type_params = PyObject_GetAttr(owner, &_Py_ID(__type_params__));
if (type_params == NULL) {
return NULL;
}
PyObject *res = PyObject_GetItem(type_params, index);
Py_DECREF(type_params);
return res;
}


static PyMethodDef typing_methods[] = {
_TYPING__IDFUNC_METHODDEF
_TYPING__RESTORE_ANONYMOUS_TYPEPARAM_METHODDEF
{NULL, NULL, 0, NULL}
};

Expand Down
35 changes: 34 additions & 1 deletion Modules/clinic/_typingmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading