This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: typing.NamedTuple instances are not picklable.
Type: Stage:
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: ashwch, gvanrossum, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

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:24adminsetgithub: 69851
2015-11-20 16:34:02python-devsetmessages: + msg254997
2015-11-20 16:21:12gvanrossumsetmessages: + msg254993
2015-11-20 15:53:48serhiy.storchakasetfiles: + test_typing_pickle.patch
nosy: + serhiy.storchaka
messages: + msg254988

2015-11-19 16:21:49gvanrossumsetstatus: open -> closed
versions: + Python 3.6
messages: + msg254909

assignee: gvanrossum
resolution: fixed
2015-11-19 16:17:23python-devsetnosy: + python-dev
messages: + msg254908
2015-11-19 08:57:47ashwchsetfiles: + issue25665.patch
keywords: + patch
messages: + msg254889
2015-11-19 08:17:24serhiy.storchakasetnosy: + gvanrossum, rhettinger
2015-11-19 08:06:26ashwchcreate