diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-10-08 13:29:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-08 13:29:52 (GMT) |
commit | 13abda41003daf599587991d8291f0dacf6e9519 (patch) | |
tree | f86d3dea6c84355c49f36bea32bf1ed846f8b447 /Lib | |
parent | b690a2759e62d9ee0b6ea1b20e8f7e4b2cdbf8bb (diff) | |
download | cpython-13abda41003daf599587991d8291f0dacf6e9519.zip cpython-13abda41003daf599587991d8291f0dacf6e9519.tar.gz cpython-13abda41003daf599587991d8291f0dacf6e9519.tar.bz2 |
bpo-38405: Make nested subclasses of typing.NamedTuple pickleable. (GH-16641)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_typing.py | 28 | ||||
-rw-r--r-- | Lib/typing.py | 2 |
2 files changed, 22 insertions, 8 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 97e6d88..49417ef 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3468,6 +3468,9 @@ class NewTypeTests(BaseTestCase): class NamedTupleTests(BaseTestCase): + class NestedEmployee(NamedTuple): + name: str + cool: int def test_basics(self): Emp = NamedTuple('Emp', [('name', str), ('id', int)]) @@ -3587,14 +3590,25 @@ class XMethBad2(NamedTuple): with self.assertRaises(TypeError): NamedTuple('Emp', fields=[('name', str), ('id', int)]) - def test_pickle(self): + def test_copy_and_pickle(self): global Emp # pickle wants to reference the class by name - Emp = NamedTuple('Emp', [('name', str), ('id', int)]) - jane = Emp('jane', 37) - for proto in range(pickle.HIGHEST_PROTOCOL + 1): - z = pickle.dumps(jane, proto) - jane2 = pickle.loads(z) - self.assertEqual(jane2, jane) + Emp = NamedTuple('Emp', [('name', str), ('cool', int)]) + for cls in Emp, CoolEmployee, self.NestedEmployee: + with self.subTest(cls=cls): + jane = cls('jane', 37) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + z = pickle.dumps(jane, proto) + jane2 = pickle.loads(z) + self.assertEqual(jane2, jane) + self.assertIsInstance(jane2, cls) + + jane2 = copy(jane) + self.assertEqual(jane2, jane) + self.assertIsInstance(jane2, cls) + + jane2 = deepcopy(jane) + self.assertEqual(jane2, jane) + self.assertIsInstance(jane2, cls) class TypedDictTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index 2c75a76..0e842ff 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1593,7 +1593,7 @@ _prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__', '_fields', '_field_defaults', '_field_types', '_make', '_replace', '_asdict', '_source') -_special = ('__module__', '__name__', '__qualname__', '__annotations__') +_special = ('__module__', '__name__', '__annotations__') class NamedTupleMeta(type): |