summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido van Rossum <guido@dropbox.com>2015-11-19 08:16:52 -0800
committerGuido van Rossum <guido@dropbox.com>2015-11-19 08:16:52 -0800
commit1b2bd70f98a24bbf69939fbddcb7a52d923d18aa (patch)
tree01b190fbf58f419bdb6e2478b368d1b037fe637c
parentca0f0a6badce6ab66d0b1e2323d3ecc59cb02f6c (diff)
parent557d1eb0f3ccb9b0bea685a1883dd249cbe6d23a (diff)
downloadcpython-git-1b2bd70f98a24bbf69939fbddcb7a52d923d18aa.tar.gz
Issue #25665: Make NamedTuple picklable. (Merge 3.5->3.6)
-rw-r--r--Lib/test/test_typing.py8
-rw-r--r--Lib/typing.py5
2 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index ae138c6598..6ddaba94d5 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1163,6 +1163,14 @@ class NamedTupleTests(TestCase):
assert Emp._fields == ('name', 'id')
assert Emp._field_types == dict(name=str, id=int)
+ def test_pickle(self):
+ global Emp # pickle wants to reference the class by name
+ Emp = NamedTuple('Emp', [('name', str), ('id', int)])
+ jane = Emp('jane', 37)
+ z = pickle.dumps(jane)
+ jane2 = pickle.loads(z)
+ assert jane == jane2
+
class IOTests(TestCase):
diff --git a/Lib/typing.py b/Lib/typing.py
index 49c4a06973..1757f13822 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1479,6 +1479,11 @@ 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)
+ # Set the module to the caller's module (otherwise it'd be 'typing').
+ try:
+ cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
+ except (AttributeError, ValueError):
+ pass
return cls