Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9157545

Browse files
committed
merge
2 parents ce50802 + 7a3602e commit 9157545

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

Doc/library/collections.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,10 @@ field names, the method and attribute names start with an underscore.
842842
.. method:: somenamedtuple._asdict()
843843

844844
Return a new :class:`OrderedDict` which maps field names to their corresponding
845-
values. Note, this method is no longer needed now that the same effect can
846-
be achieved by using the built-in :func:`vars` function::
845+
values::
847846

848-
>>> vars(p)
847+
>>> p = Point(x=11, y=22)
848+
>>> p._asdict()
849849
OrderedDict([('x', 11), ('y', 22)])
850850

851851
.. versionchanged:: 3.1

Lib/collections/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,23 +320,14 @@ def __repr__(self):
320320
'Return a nicely formatted representation string'
321321
return self.__class__.__name__ + '({repr_fmt})' % self
322322
323-
@property
324-
def __dict__(self):
325-
'A new OrderedDict mapping field names to their values'
326-
return OrderedDict(zip(self._fields, self))
327-
328323
def _asdict(self):
329324
'Return a new OrderedDict which maps field names to their values.'
330-
return self.__dict__
325+
return OrderedDict(zip(self._fields, self))
331326
332327
def __getnewargs__(self):
333328
'Return self as a plain tuple. Used by copy and pickle.'
334329
return tuple(self)
335330
336-
def __getstate__(self):
337-
'Exclude the OrderedDict from pickling'
338-
return None
339-
340331
{field_defs}
341332
"""
342333

Lib/test/test_collections.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ def test_instance(self):
257257
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
258258
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
259259
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
260-
self.assertEqual(vars(p), p._asdict()) # verify that vars() works
261260

262261
try:
263262
p._replace(x=1, error=2)
@@ -412,6 +411,17 @@ def test_source(self):
412411
globals().pop('NTColor', None) # clean-up after this test
413412

414413

414+
def test_namedtuple_subclass_issue_24931(self):
415+
class Point(namedtuple('_Point', ['x', 'y'])):
416+
pass
417+
418+
a = Point(3, 4)
419+
self.assertEqual(a._asdict(), OrderedDict([('x', 3), ('y', 4)]))
420+
421+
a.w = 5
422+
self.assertEqual(a.__dict__, {'w': 5})
423+
424+
415425
################################################################################
416426
### Abstract Base Classes
417427
################################################################################

0 commit comments

Comments
 (0)