]> granicus.if.org Git - python/commitdiff
Issue #25628: Make namedtuple "rename" and "verbose" parameters keyword-only.
authorRaymond Hettinger <python@rcn.com>
Tue, 16 Aug 2016 17:55:43 +0000 (10:55 -0700)
committerRaymond Hettinger <python@rcn.com>
Tue, 16 Aug 2016 17:55:43 +0000 (10:55 -0700)
Doc/library/collections.rst
Lib/collections/__init__.py
Lib/test/test_collections.py
Misc/NEWS

index 4503a07d32d9d8ee9c06841983b9c7a2c30e1522..a6c9be633347babef1eaff30f8e6c831522c859b 100644 (file)
@@ -763,7 +763,7 @@ Named tuples assign meaning to each position in a tuple and allow for more reada
 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 name instead of position index.
     a namedtuple.
 
     .. versionchanged:: 3.1
-        Added support for *rename*.
+       Added support for *rename*.
+
+    .. versionchanged:: 3.6
+       The *verbose* and *rename* parameters became
+       :ref:`keyword-only arguments <keyword-only_parameter>`.
 
 
 .. doctest::
index b9419506e93d751eb55b5bcc4144cf1ae4c82246..f465e7477079eb6f100045ee85269431e5504ccc 100644 (file)
@@ -353,7 +353,7 @@ _field_template = '''\
     {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'])
index a80c49c2788407e87660cfb2d2f9b03ea07b3473..c4c0a169fe81aaa606dfd19aa626c9351749fac6 100644 (file)
@@ -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
+        with support.captured_stdout() as template:
+            NT = namedtuple('NT', ['x', 'y'], verbose=True)
+        self.assertIn('class NT', NT._source)
+        with self.assertRaises(TypeError):
+            NT = namedtuple('NT', ['x', 'y'], True)
+
+        NT = namedtuple('NT', ['abc', 'def'], rename=True)
+        self.assertEqual(NT._fields, ('abc', '_1'))
+        with self.assertRaises(TypeError):
+            NT = namedtuple('NT', ['abc', 'def'], False, True)
 
     def test_namedtuple_subclass_issue_24931(self):
         class Point(namedtuple('_Point', ['x', 'y'])):
index 1e2a7b76b5d73cc8efcb6c6ffe1711e0bf369e11..1d1bbd860f3e13751c52dd6c18015baa35030e34 100644 (file)
--- 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
+  are now keyword-only.
+
 - Issue #12345: Add mathemathical constant tau to math and cmath. See also
   PEP 628.