classification
Title: In the namedtuple documentation, mention that typename should match the variable name for the class to be pickle-able
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: alegonz, docs@python, miss-islington, rhettinger
Priority: normal Keywords: patch

Created on 2020-11-15 09:14 by alegonz, last changed 2020-11-20 21:27 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23305 merged rhettinger, 2020-11-15 21:29
PR 23429 merged miss-islington, 2020-11-20 20:49
Messages (4)
msg381007 - (view) Author: Alejandro Gonzalez (alegonz) * Date: 2020-11-15 09:14
I think the namedtuple documentation should mention that, for classes created with it to be pickle-able, the typename argument must match the name of the variable the class is being assigned to. Otherwise you get an error like this:

----------------------------------------------
>>> Foo = namedtuple("Bar", "x,y")
>>> pickle.dumps(Foo(1, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class '__main__.Bar'>: attribute lookup Bar on __main__ failed
----------------------------------------------

While it is indeed odd to do such naming in the first place, it should be admissible in other circumstances not involving pickling, and it is not obvious that pickling won't work if you do so.

The pickle documentation does mention this, though:

[...]Similarly, classes are pickled by named reference, so the same restrictions in the unpickling environment apply.[...]

but for someone who encounters this error it might be difficult to figure it out from that passage.

The added documentation in namedtuple could include a pointer to that section of pickle's documentation.
msg381024 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-15 18:32
I'll add a brief note to the named tuple docs.

FWIW, this is a special case of the more general issue that pickling errors tend to be inscrutable.
msg381505 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-20 20:49
New changeset 9fc319dc033fa32d298fe1c3f171b3d011ac04f0 by Raymond Hettinger in branch 'master':
bpo-42360: Add advice to help avoid pickling issues. (GH-23305)
https://github.com/python/cpython/commit/9fc319dc033fa32d298fe1c3f171b3d011ac04f0
msg381508 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-20 21:19
New changeset f552f4b2d635ae031e154374ba3a609c63d09d2b by Miss Islington (bot) in branch '3.9':
bpo-42360: Add advice to help avoid pickling issues. (GH-23305) (GH-23429)
https://github.com/python/cpython/commit/f552f4b2d635ae031e154374ba3a609c63d09d2b
History
Date User Action Args
2020-11-20 21:27:21rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-11-20 21:19:53rhettingersetmessages: + msg381508
2020-11-20 20:49:48miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22320
2020-11-20 20:49:36rhettingersetmessages: + msg381505
2020-11-15 21:29:07rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request22198
2020-11-15 18:32:46rhettingersetassignee: docs@python -> rhettinger

messages: + msg381024
nosy: + rhettinger
2020-11-15 09:14:25alegonzcreate