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

Skip to content

Commit 7a3602e

Browse files
committed
Issue #24931: Resolve __dict__ conflict in namedtuple subclasses.
1 parent 1a83746 commit 7a3602e

4 files changed

Lines changed: 19 additions & 14 deletions

File tree

Doc/library/collections.rst

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

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

822-
>>> vars(p)
821+
>>> p = Point(x=11, y=22)
822+
>>> p._asdict()
823823
OrderedDict([('x', 11), ('y', 22)])
824824

825825
.. versionchanged:: 3.1

Lib/collections/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,14 @@ def __repr__(self):
272272
'Return a nicely formatted representation string'
273273
return self.__class__.__name__ + '({repr_fmt})' % self
274274
275-
@property
276-
def __dict__(self):
277-
'A new OrderedDict mapping field names to their values'
278-
return OrderedDict(zip(self._fields, self))
279-
280275
def _asdict(self):
281276
'Return a new OrderedDict which maps field names to their values.'
282-
return self.__dict__
277+
return OrderedDict(zip(self._fields, self))
283278
284279
def __getnewargs__(self):
285280
'Return self as a plain tuple. Used by copy and pickle.'
286281
return tuple(self)
287282
288-
def __getstate__(self):
289-
'Exclude the OrderedDict from pickling'
290-
return None
291-
292283
{field_defs}
293284
"""
294285

Lib/test/test_collections.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ def test_instance(self):
225225
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
226226
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
227227
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
228-
self.assertEqual(vars(p), p._asdict()) # verify that vars() works
229228

230229
try:
231230
p._replace(x=1, error=2)
@@ -380,6 +379,17 @@ def test_source(self):
380379
globals().pop('NTColor', None) # clean-up after this test
381380

382381

382+
def test_namedtuple_subclass_issue_24931(self):
383+
class Point(namedtuple('_Point', ['x', 'y'])):
384+
pass
385+
386+
a = Point(3, 4)
387+
self.assertEqual(a._asdict(), OrderedDict([('x', 3), ('y', 4)]))
388+
389+
a.w = 5
390+
self.assertEqual(a.__dict__, {'w': 5})
391+
392+
383393
################################################################################
384394
### Abstract Base Classes
385395
################################################################################

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ Library
7878
- Issue #21112: Fix regression in unittest.expectedFailure on subclasses.
7979
Patch from Berker Peksag.
8080

81+
- Issue #24931: Instances of subclasses of namedtuples have their own __dict__
82+
which breaks the inherited __dict__ property and breaks the _asdict() method.
83+
Removed the __dict__ property to prevent the conflict and fixed _asdict().
84+
8185
- Issue #24764: cgi.FieldStorage.read_multi() now ignores the Content-Length
8286
header in part headers. Patch written by Peter Landry and reviewed by Pierre
8387
Quentel.

0 commit comments

Comments
 (0)