Issue 25665: typing.NamedTuple instances are not picklable. (original) (raw)

Created on 2015-11-19 08:06 by ashwch, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue25665.patch ashwch,2015-11-19 08:57 review
test_typing_pickle.patch serhiy.storchaka,2015-11-20 15:53 review
Messages (7)
msg254883 - (view) Author: Ashwini Chaudhary (ashwch) * Date: 2015-11-19 08:06
Currently namedtuple(https://hg.python.org/cpython/file/3.5/Lib/collections/__init__.py#l418) sets the `__module__` attribute by looking up `__name__` in calling frame's globals. As in the case of `typing.NamedTuple` it is always going to be 'typing' pickle will raise an error. Instead of this `typing.NamedTuple` should override the `__module__` attribute itself because it has info about the actual caller frame. Something like this should work fine: ``` def NamedTuple(typename, fields): fields = [(n, t) for n, t in fields] cls = collections.namedtuple(typename, [n for n, t in fields]) cls._field_types = dict(fields) try: cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__') except (AttributeError, ValueError): pass return cls ``` Related: http://stackoverflow.com/q/33796490/846892
msg254889 - (view) Author: Ashwini Chaudhary (ashwch) * Date: 2015-11-19 08:57
Attached patch.
msg254908 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-11-19 16:17
New changeset 33df0056c148 by Guido van Rossum in branch '3.5': Issue #25665: Make NamedTuple picklable. https://hg.python.org/cpython/rev/33df0056c148 New changeset 8a32d44b8359 by Guido van Rossum in branch 'default': Issue #25665: Make NamedTuple picklable. (Merge 3.5->3.6) https://hg.python.org/cpython/rev/8a32d44b8359
msg254909 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2015-11-19 16:21
Fixed it! Thanks for the report *and* the patch. I wrote a different test though.
msg254988 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-11-20 15:53
The test tests pickling only with default protocol. It would be better to test pickling with all supported protocols. Why special TestCase methods to check for and report failures are not used in test_typing?
msg254993 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2015-11-20 16:21
Serhiy, feel free to commit that patch. I was just being lazy. Also, I was using assert instead of self.assertEquals etc. out of laziness (and because at Dropbox people have got the py.test religion and prefer to use assert statements -- but I'm not keen to do this for the stdlib).
msg254997 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-11-20 16:34
New changeset 4f30b0d47c24 by Serhiy Storchaka in branch '3.5': Issue #25665: Test pickling with all protocols in test_typing. https://hg.python.org/cpython/rev/4f30b0d47c24 New changeset 9e65015582a5 by Serhiy Storchaka in branch 'default': Issue #25665: Test pickling with all protocols in test_typing. https://hg.python.org/cpython/rev/9e65015582a5
History
Date User Action Args
2022-04-11 14:58:24 admin set github: 69851
2015-11-20 16:34:02 python-dev set messages: +
2015-11-20 16:21:12 gvanrossum set messages: +
2015-11-20 15:53:48 serhiy.storchaka set files: + test_typing_pickle.patchnosy: + serhiy.storchakamessages: +
2015-11-19 16:21:49 gvanrossum set status: open -> closedversions: + Python 3.6messages: + assignee: gvanrossumresolution: fixed
2015-11-19 16:17:23 python-dev set nosy: + python-devmessages: +
2015-11-19 08:57:47 ashwch set files: + issue25665.patchkeywords: + patchmessages: +
2015-11-19 08:17:24 serhiy.storchaka set nosy: + gvanrossum, rhettinger
2015-11-19 08:06:26 ashwch create