Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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 21 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 1 Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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 2 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);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion 3 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 1 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 37 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()
tom-pytel marked this conversation as resolved.
Show resolved Hide resolved
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 29 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 35 Modules/clinic/_typingmodule.c.h

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

Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.