cpython: 8a32d44b8359 (original) (raw)
Mercurial > cpython
changeset 99213:8a32d44b8359
Issue #25665: Make NamedTuple picklable. (Merge 3.5->3.6) [#25665]
Guido van Rossum guido@dropbox.com | |
---|---|
date | Thu, 19 Nov 2015 08:16:52 -0800 |
parents | 312a2220100c(current diff)33df0056c148(diff) |
children | 2ebe03a94f8f |
files | Lib/test/test_typing.py Lib/typing.py |
diffstat | 2 files changed, 13 insertions(+), 0 deletions(-)[+] [-] Lib/test/test_typing.py 8 Lib/typing.py 5 |
line wrap: on
line diff
--- 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[](#l1.8)
Emp = NamedTuple('Emp', [('name', str), ('id', int)])[](#l1.9)
jane = Emp('jane', 37)[](#l1.10)
z = pickle.dumps(jane)[](#l1.11)
jane2 = pickle.loads(z)[](#l1.12)
assert jane == jane2[](#l1.13)
--- 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)