[Python-checkins] bpo-36321: Fix misspelled attribute in namedtuple() (GH-12375)

Raymond Hettinger webhook-mailer at python.org
Mon Mar 18 03:27:43 EDT 2019


https://github.com/python/cpython/commit/23581c018fceb607fe829a41c6fbe81b4d502cab
commit: 23581c018fceb607fe829a41c6fbe81b4d502cab
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-03-18T00:27:39-07:00
summary:

bpo-36321: Fix misspelled attribute in namedtuple() (GH-12375)

files:
A Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst
M Doc/library/collections.rst
M Lib/collections/__init__.py
M Lib/test/test_collections.py

diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index daa741428a01..64de970fec94 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -953,14 +953,14 @@ field names, the method and attribute names start with an underscore.
         >>> Pixel(11, 22, 128, 255, 0)
         Pixel(x=11, y=22, red=128, green=255, blue=0)
 
-.. attribute:: somenamedtuple._fields_defaults
+.. attribute:: somenamedtuple._field_defaults
 
    Dictionary mapping field names to default values.
 
    .. doctest::
 
         >>> Account = namedtuple('Account', ['type', 'balance'], defaults=[0])
-        >>> Account._fields_defaults
+        >>> Account._field_defaults
         {'balance': 0}
         >>> Account('premium')
         Account(type='premium', balance=0)
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 632a509d316c..cff75a48d627 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -445,6 +445,8 @@ def __getnewargs__(self):
         '__doc__': f'{typename}({arg_list})',
         '__slots__': (),
         '_fields': field_names,
+        '_field_defaults': field_defaults,
+        # alternate spelling for backward compatiblity
         '_fields_defaults': field_defaults,
         '__new__': __new__,
         '_make': _make,
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index dad1b6cd7fb3..1f619bcdac92 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -248,18 +248,18 @@ def test_factory(self):
 
     def test_defaults(self):
         Point = namedtuple('Point', 'x y', defaults=(10, 20))              # 2 defaults
-        self.assertEqual(Point._fields_defaults, {'x': 10, 'y': 20})
+        self.assertEqual(Point._field_defaults, {'x': 10, 'y': 20})
         self.assertEqual(Point(1, 2), (1, 2))
         self.assertEqual(Point(1), (1, 20))
         self.assertEqual(Point(), (10, 20))
 
         Point = namedtuple('Point', 'x y', defaults=(20,))                 # 1 default
-        self.assertEqual(Point._fields_defaults, {'y': 20})
+        self.assertEqual(Point._field_defaults, {'y': 20})
         self.assertEqual(Point(1, 2), (1, 2))
         self.assertEqual(Point(1), (1, 20))
 
         Point = namedtuple('Point', 'x y', defaults=())                     # 0 defaults
-        self.assertEqual(Point._fields_defaults, {})
+        self.assertEqual(Point._field_defaults, {})
         self.assertEqual(Point(1, 2), (1, 2))
         with self.assertRaises(TypeError):
             Point(1)
@@ -276,21 +276,21 @@ def test_defaults(self):
             Point = namedtuple('Point', 'x y', defaults=False)
 
         Point = namedtuple('Point', 'x y', defaults=None)                   # default is None
-        self.assertEqual(Point._fields_defaults, {})
+        self.assertEqual(Point._field_defaults, {})
         self.assertIsNone(Point.__new__.__defaults__, None)
         self.assertEqual(Point(10, 20), (10, 20))
         with self.assertRaises(TypeError):                                  # catch too few args
             Point(10)
 
         Point = namedtuple('Point', 'x y', defaults=[10, 20])               # allow non-tuple iterable
-        self.assertEqual(Point._fields_defaults, {'x': 10, 'y': 20})
+        self.assertEqual(Point._field_defaults, {'x': 10, 'y': 20})
         self.assertEqual(Point.__new__.__defaults__, (10, 20))
         self.assertEqual(Point(1, 2), (1, 2))
         self.assertEqual(Point(1), (1, 20))
         self.assertEqual(Point(), (10, 20))
 
         Point = namedtuple('Point', 'x y', defaults=iter([10, 20]))         # allow plain iterator
-        self.assertEqual(Point._fields_defaults, {'x': 10, 'y': 20})
+        self.assertEqual(Point._field_defaults, {'x': 10, 'y': 20})
         self.assertEqual(Point.__new__.__defaults__, (10, 20))
         self.assertEqual(Point(1, 2), (1, 2))
         self.assertEqual(Point(1), (1, 20))
diff --git a/Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst b/Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst
new file mode 100644
index 000000000000..eea6f5418484
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst
@@ -0,0 +1,5 @@
+collections.namedtuple() misspelled the name of an attribute.  To be
+consistent with typing.NamedTuple, the attribute name should have been
+"_field_defaults" instead of "_fields_defaults".  For backwards
+compatibility, both spellings are now created.  The misspelled version may
+be removed in the future.



More information about the Python-checkins mailing list