Skip to content

bpo-34363: Handle namedtuples fields correctly in asdict() and astuple(). #8728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,14 @@ def _asdict_inner(obj, dict_factory):
value = _asdict_inner(getattr(obj, f.name), dict_factory)
result.append((f.name, value))
return dict_factory(result)
elif isinstance(obj, (list, tuple)):
elif type(obj) in (list, tuple):
# Actual list or tuple.
return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
elif isinstance(obj, (list, tuple)):
# Subclass of list or tuple. Probably a namedtuple, whose
# __new__ works differently than tuple.__new__ does with
# respect to iterators.
return type(obj)(*[_asdict_inner(v, dict_factory) for v in obj])
elif isinstance(obj, dict):
return type(obj)((_asdict_inner(k, dict_factory), _asdict_inner(v, dict_factory))
for k, v in obj.items())
Expand Down Expand Up @@ -1060,8 +1066,14 @@ def _astuple_inner(obj, tuple_factory):
value = _astuple_inner(getattr(obj, f.name), tuple_factory)
result.append(value)
return tuple_factory(result)
elif isinstance(obj, (list, tuple)):
elif type(obj) in (list, tuple):
# Actual list or tuple.
return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
elif isinstance(obj, (list, tuple)):
# Subclass of list or tuple. Probably a namedtuple, whose
# __new__ works differently than tuple.__new__ does with
# respect to iterators.
return type(obj)(*[_astuple_inner(v, tuple_factory) for v in obj])
elif isinstance(obj, dict):
return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
for k, v in obj.items())
Expand Down
74 changes: 73 additions & 1 deletion Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import inspect
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, NamedTuple
from collections import deque, OrderedDict, namedtuple
from functools import total_ordering

Expand Down Expand Up @@ -1281,6 +1281,42 @@ class C:
self.assertEqual(asdict(c), {'x': 42, 'y': 2})
self.assertIs(type(asdict(c)), dict)

def test_helper_asdict_namedtuple(self):
nt1 = namedtuple('nt1', 'f')
nt2 = namedtuple('nt2', 'f1 f2')

class NT1(NamedTuple):
f: Any

class NT2(NamedTuple):
f1: Any
f2: Any

# Test with both namedtuple and NamedTuple.
for type1, type2 in ((nt1, nt2), (NT1, NT2)):
with self.subTest(type1=type1, type2=type2):
@dataclass
class C:
i1: int
nt1: type1
nt2: type2

inst1 = type1((1,))
inst2 = type2((3,), (5, 6))
c = C(3, inst1, inst2)
d = asdict(c)
self.assertEqual(d, {'i1': 3, 'nt1': inst1, 'nt2': inst2})
# Make sure we copied these objects and aren't using the
# originals.
self.assertIsNot(d['nt1'], inst1)
self.assertIsNot(d['nt2'], inst2)
# Due to the way that namedtuples compare to tuples, check
# that dict values can be accessed by field name (that is,
# they're namedtuples not tuples).
self.assertEqual(d['nt1'].f, (1,))
self.assertEqual(d['nt2'].f1, (3,))
self.assertEqual(d['nt2'].f2, (5, 6))

def test_helper_asdict_raises_on_classes(self):
# asdict() should raise on a class object.
@dataclass
Expand Down Expand Up @@ -1394,6 +1430,42 @@ class C:
self.assertEqual(astuple(c), (1, 42))
self.assertIs(type(astuple(c)), tuple)

def test_helper_astuple_namedtuple(self):
nt1 = namedtuple('nt1', 'f')
nt2 = namedtuple('nt2', 'f1 f2')

class NT1(NamedTuple):
f: Any

class NT2(NamedTuple):
f1: Any
f2: Any

# Test with both namedtuple and NamedTuple.
for type1, type2 in ((nt1, nt2), (NT1, NT2)):
with self.subTest(type1=type1, type2=type2):
@dataclass
class C:
i1: int
nt1: type1
nt2: type2

inst1 = type1((1,))
inst2 = type2((3,), (5, 6))
c = C(3, inst1, inst2)
t = astuple(c)
self.assertEqual(t, (3, inst1, inst2))
# Make sure we copied these objects and aren't using the
# originals.
self.assertIsNot(t[1], inst1)
self.assertIsNot(t[2], inst2)
# Due to the way that namedtuples compare to tuples, check
# that dict values can be accessed by field name (that is,
# they're namedtuples not tuples).
self.assertEqual(t[1].f, (1,))
self.assertEqual(t[2].f1, (3,))
self.assertEqual(t[2].f2, (5, 6))

def test_helper_astuple_raises_on_classes(self):
# astuple() should raise on a class object.
@dataclass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle namedtuple fields correct in dataclasses.astuple() and .asdict().