cpython: f086d6c9d9f6 (original) (raw)
Mercurial > cpython
changeset 102703:f086d6c9d9f6
Issue #25628: Make namedtuple "rename" and "verbose" parameters keyword-only. [#25628]
Raymond Hettinger python@rcn.com | |
---|---|
date | Tue, 16 Aug 2016 10:55:43 -0700 |
parents | ad141164c792 |
children | 61403b862793 |
files | Doc/library/collections.rst Lib/collections/__init__.py Lib/test/test_collections.py Misc/NEWS |
diffstat | 4 files changed, 22 insertions(+), 3 deletions(-)[+] [-] Doc/library/collections.rst 8 Lib/collections/__init__.py 2 Lib/test/test_collections.py 12 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -763,7 +763,7 @@ Named tuples assign meaning to each posi self-documenting code. They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index. -.. function:: namedtuple(typename, field_names, verbose=False, rename=False) +.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False) Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as @@ -799,7 +799,11 @@ they add the ability to access fields by a namedtuple. .. versionchanged:: 3.1
Added support for *rename*.[](#l1.16)
Added support for *rename*.[](#l1.17)
- .. versionchanged:: 3.6
The *verbose* and *rename* parameters became[](#l1.20)
:ref:`keyword-only arguments <keyword-only_parameter>`.[](#l1.21)
--- a/Lib/collections/init.py +++ b/Lib/collections/init.py @@ -353,7 +353,7 @@ class {typename}(tuple): {name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}') ''' -def namedtuple(typename, field_names, verbose=False, rename=False): +def namedtuple(typename, field_names, *, verbose=False, rename=False): """Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', ['x', 'y'])
--- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -412,6 +412,18 @@ class TestNamedTuple(unittest.TestCase): self.assertEqual(NTColor._fields, ('red', 'green', 'blue')) globals().pop('NTColor', None) # clean-up after this test
- def test_keyword_only_arguments(self):
# See issue 25628[](#l3.8)
with support.captured_stdout() as template:[](#l3.9)
NT = namedtuple('NT', ['x', 'y'], verbose=True)[](#l3.10)
self.assertIn('class NT', NT._source)[](#l3.11)
with self.assertRaises(TypeError):[](#l3.12)
NT = namedtuple('NT', ['x', 'y'], True)[](#l3.13)
NT = namedtuple('NT', ['abc', 'def'], rename=True)[](#l3.15)
self.assertEqual(NT._fields, ('abc', '_1'))[](#l3.16)
with self.assertRaises(TypeError):[](#l3.17)
NT = namedtuple('NT', ['abc', 'def'], False, True)[](#l3.18)
def test_namedtuple_subclass_issue_24931(self): class Point(namedtuple('_Point', ['x', 'y'])):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -73,6 +73,9 @@ Library to ref count problem introduced in code for Issue #27038 in 3.6.0a3. Patch by Xiang Zhang. +- Issue #25628: The verbose and rename parameters for collections.namedtuple