summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Doc/library/collections.rst8
-rw-r--r--Lib/collections/__init__.py2
-rw-r--r--Lib/test/test_collections.py12
-rw-r--r--Misc/NEWS3
4 files changed, 22 insertions, 3 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 4503a07..a6c9be6 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -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::
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index b941950..f465e74 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -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'])
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index a80c49c..c4c0a16 100644
--- 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
+ 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'])):
diff --git a/Misc/NEWS b/Misc/NEWS
index 1e2a7b7..1d1bbd8 100644
--- 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.