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

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


https://github.com/python/cpython/commit/bedfbc790e18f14cfdd59cf27d27bb86518dc3fc
commit: bedfbc790e18f14cfdd59cf27d27bb86518dc3fc
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2019-03-18T00:48:02-07:00
summary:

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

(cherry picked from commit 23581c018fceb607fe829a41c6fbe81b4d502cab)

Co-authored-by: Raymond Hettinger <rhettinger at users.noreply.github.com>

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 febaa4bf4620..1616585b654b 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -941,14 +941,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 9a753db71cae..1c69b1b50961 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -443,6 +443,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 2e7c1996d118..00b33ce27674 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -249,18 +249,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)
@@ -277,21 +277,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