Skip to content

Commit 3d89057

Browse files
committed
Fix named tuples to work with vars().
1 parent 9028928 commit 3d89057

File tree

4 files changed

+7
-1
lines changed

4 files changed

+7
-1
lines changed

Doc/library/collections.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ they add the ability to access fields by name instead of position index.
622622
def _asdict(self):
623623
'Return a new OrderedDict which maps field names to their values'
624624
return OrderedDict(zip(self._fields, self))
625+
<BLANKLINE>
626+
__dict__ = property(_asdict)
625627
<BLANKLINE>
626628
def _replace(_self, **kwds):
627629
'Return a new Point object replacing specified fields with new values'

Lib/collections.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ def _asdict(self):
268268
'Return a new OrderedDict which maps field names to their values'
269269
return OrderedDict(zip(self._fields, self))
270270
271+
__dict__ = property(_asdict)
272+
271273
def _replace(_self, **kwds):
272274
'Return a new {typename} object replacing specified fields with new values'
273275
result = _self._make(map(kwds.pop, {field_names!r}, _self))

Lib/test/test_collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ def test_instance(self):
180180
self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument
181181
self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument
182182
self.assertEqual(repr(p), 'Point(x=11, y=22)')
183-
self.assertNotIn('__dict__', dir(p)) # verify instance has no dict
184183
self.assertNotIn('__weakref__', dir(p))
185184
self.assertEqual(p, Point._make([11, 22])) # test _make classmethod
186185
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
187186
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
188187
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
188+
self.assertEqual(vars(p), p._asdict()) # verify that vars() works
189189

190190
try:
191191
p._replace(x=1, error=2)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Named tuples now work correctly with vars().
26+
2527
- Issue #12085: Fix an attribute error in subprocess.Popen destructor if the
2628
constructor has failed, e.g. because of an undeclared keyword argument. Patch
2729
written by Oleg Oshmyan.

0 commit comments

Comments
 (0)