bpo-36321: Fix misspelled attribute in namedtuple() (GH-12375)
This commit is contained in:
parent
6a16b18224
commit
23581c018f
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue