You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
typing.NamedTuple instances are not picklable. #69851
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/gvanrossum'closed_at=<Date2015-11-19.16:21:49.096>created_at=<Date2015-11-19.08:06:26.539>labels= ['library']
title='typing.NamedTuple instances are not picklable.'updated_at=<Date2015-11-20.16:34:02.151>user='https://github.com/ashwch'
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
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).
Activity
ashwch commentedon Nov 19, 2015
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 oftyping.NamedTupleit is always going to be 'typing' pickle will raise an error.Instead of this
typing.NamedTupleshould override the__module__attribute itself because it has info about the actual caller frame.Something like this should work fine:
Related: http://stackoverflow.com/q/33796490/846892
ashwch commentedon Nov 19, 2015
Attached patch.
python-dev commentedon Nov 19, 2015
New changeset 33df0056c148 by Guido van Rossum in branch '3.5':
Issue bpo-25665: Make NamedTuple picklable.
https://hg.python.org/cpython/rev/33df0056c148
New changeset 8a32d44b8359 by Guido van Rossum in branch 'default':
Issue bpo-25665: Make NamedTuple picklable. (Merge 3.5->3.6)
https://hg.python.org/cpython/rev/8a32d44b8359
gvanrossum commentedon Nov 19, 2015
Fixed it! Thanks for the report *and* the patch. I wrote a different test though.
serhiy-storchaka commentedon Nov 20, 2015
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?
gvanrossum commentedon Nov 20, 2015
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).
python-dev commentedon Nov 20, 2015
New changeset 4f30b0d47c24 by Serhiy Storchaka in branch '3.5':
Issue bpo-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 bpo-25665: Test pickling with all protocols in test_typing.
https://hg.python.org/cpython/rev/9e65015582a5