diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-03-18 07:27:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-18 07:27:39 (GMT) |
commit | 23581c018fceb607fe829a41c6fbe81b4d502cab (patch) | |
tree | 9c8f76fb525ad1ac974a08ebac92812db7feeefa | |
parent | 6a16b18224fa98f6d192aa5014affeccc0376eb3 (diff) | |
download | cpython-23581c018fceb607fe829a41c6fbe81b4d502cab.zip cpython-23581c018fceb607fe829a41c6fbe81b4d502cab.tar.gz cpython-23581c018fceb607fe829a41c6fbe81b4d502cab.tar.bz2 |
bpo-36321: Fix misspelled attribute in namedtuple() (GH-12375)
-rw-r--r-- | Doc/library/collections.rst | 4 | ||||
-rw-r--r-- | Lib/collections/__init__.py | 2 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 12 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst | 5 |
4 files changed, 15 insertions, 8 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index daa7414..64de970 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 632a509..cff75a4 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -445,6 +445,8 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non '__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 dad1b6c..1f619bc 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -248,18 +248,18 @@ class TestNamedTuple(unittest.TestCase): 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 @@ class TestNamedTuple(unittest.TestCase): 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 0000000..eea6f54 --- /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. |