diff options
author | Guido van Rossum <guido@python.org> | 2016-11-15 17:48:06 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2016-11-15 17:48:06 (GMT) |
commit | 2f841442354f41f2490cef76e9ba24bfef9dfbea (patch) | |
tree | 9b1aeee58baadad475ce4f69cc782e3a237224f5 /Lib/test/test_typing.py | |
parent | 49e8f2d2040d3c15d61a713dcbe14caae41c4f0d (diff) | |
download | cpython-2f841442354f41f2490cef76e9ba24bfef9dfbea.zip cpython-2f841442354f41f2490cef76e9ba24bfef9dfbea.tar.gz cpython-2f841442354f41f2490cef76e9ba24bfef9dfbea.tar.bz2 |
Issue #28556: Allow keyword syntax for NamedTuple (Ivan Levkivskyi) (upstream #321)
Diffstat (limited to 'Lib/test/test_typing.py')
-rw-r--r-- | Lib/test/test_typing.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 0582845..12bbf16 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1865,6 +1865,20 @@ class NamedTupleTests(BaseTestCase): self.assertEqual(CoolEmployee._fields, ('name', 'cool')) self.assertEqual(CoolEmployee._field_types, dict(name=str, cool=int)) + @skipUnless(PY36, 'Python 3.6 required') + def test_namedtuple_keyword_usage(self): + LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int) + nick = LocalEmployee('Nick', 25) + self.assertIsInstance(nick, tuple) + self.assertEqual(nick.name, 'Nick') + self.assertEqual(LocalEmployee.__name__, 'LocalEmployee') + self.assertEqual(LocalEmployee._fields, ('name', 'age')) + self.assertEqual(LocalEmployee._field_types, dict(name=str, age=int)) + with self.assertRaises(TypeError): + NamedTuple('Name', [('x', int)], y=str) + with self.assertRaises(TypeError): + NamedTuple('Name', x=1, y='a') + def test_pickle(self): global Emp # pickle wants to reference the class by name Emp = NamedTuple('Emp', [('name', str), ('id', int)]) |