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 1 commit
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
Prev Previous commit
Next Next commit
requested changes
  • Loading branch information
tom-pytel committed Mar 1, 2025
commit 66ac0405fe89a8ce483ee898cd9f291f332639c5
11 changes: 5 additions & 6 deletions 11 Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,15 +1184,14 @@ 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]()
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(Generic[T]): pass
class Class6:
def meth[Baz](): return Class5[Baz]()
class Class5:
def meth[Baz](): return Class1[Baz]()


class TypeParamsPickleTest(unittest.TestCase):
Expand Down Expand Up @@ -1271,12 +1270,12 @@ def test_pickling_anonymous_typeparams(self):
unpickled = pickle.loads(pickled)
self.assertIs(unpickled, thing)

thing = Class6.meth()
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],
Class6.meth.__type_params__[0])
Class5.meth.__type_params__[0])


class TypeParamsWeakRefTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Allow pickle of instances of generic classes.
Fix pickling of generic classes instances.
13 changes: 4 additions & 9 deletions 13 Modules/_typingmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,13 @@ _typing__restore_anonymous_typeparam_impl(PyObject *module, PyObject *owner,
PyObject *index)
/*[clinic end generated code: output=00baec27dbf8d2d9 input=2f048db28d8124fb]*/
{
PyObject *ret = NULL;
PyObject *type_params = PyObject_GetAttr(owner, &_Py_ID(__type_params__));

if (type_params == NULL) {
goto done;
return NULL;
}

ret = PyObject_GetItem(type_params, index);

done:
Py_XDECREF(type_params);
return ret;
PyObject *res = PyObject_GetItem(type_params, index);
Py_DECREF(type_params);
return res;
}


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