New submission from Alejandro Gonzalez alejandrojgt@gmail.com:
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.
---------- assignee: docs@python components: Documentation messages: 381007 nosy: alegonz, docs@python priority: normal severity: normal status: open title: In the namedtuple documentation, mention that typename should match the variable name for the class to be pickle-able type: enhancement
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________
Raymond Hettinger raymond.hettinger@gmail.com added the comment:
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.
---------- assignee: docs@python -> rhettinger nosy: +rhettinger
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________
Change by Raymond Hettinger raymond.hettinger@gmail.com:
---------- keywords: +patch pull_requests: +22198 stage: -> patch review pull_request: https://github.com/python/cpython/pull/23305
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________
Raymond Hettinger raymond.hettinger@gmail.com added the comment:
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/9fc319dc033fa32d298fe1c3f171b3d011a...
----------
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________
Change by miss-islington mariatta.wijaya+miss-islington@gmail.com:
---------- nosy: +miss-islington nosy_count: 3.0 -> 4.0 pull_requests: +22320 pull_request: https://github.com/python/cpython/pull/23429
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________
Raymond Hettinger raymond.hettinger@gmail.com added the comment:
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/f552f4b2d635ae031e154374ba3a609c63d...
----------
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________
Change by Raymond Hettinger raymond.hettinger@gmail.com:
---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue42360 _______________________________________