diff options
author | Tiger <tnie@tuta.io> | 2022-10-07 00:11:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-07 00:11:59 (GMT) |
commit | c46a423a520b797c3f8c81fef19293864742755d (patch) | |
tree | 4161491118c43d512c4409df9f40e28657f74c87 /Lib/test/test_dataclasses.py | |
parent | a4b7794887929f82c532fcd055326954ff1197ce (diff) | |
download | cpython-c46a423a520b797c3f8c81fef19293864742755d.zip cpython-c46a423a520b797c3f8c81fef19293864742755d.tar.gz cpython-c46a423a520b797c3f8c81fef19293864742755d.tar.bz2 |
bpo-35540 dataclasses.asdict now supports defaultdict fields (gh-32056)
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r-- | Lib/test/test_dataclasses.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 328dcdc..637c456 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -12,9 +12,9 @@ import types import weakref import unittest from unittest.mock import Mock -from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol +from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict from typing import get_type_hints -from collections import deque, OrderedDict, namedtuple +from collections import deque, OrderedDict, namedtuple, defaultdict from functools import total_ordering import typing # Needed for the string "typing.ClassVar[int]" to work as an annotation. @@ -1677,6 +1677,23 @@ class TestCase(unittest.TestCase): self.assertIsNot(d['f'], t) self.assertEqual(d['f'].my_a(), 6) + def test_helper_asdict_defaultdict(self): + # Ensure asdict() does not throw exceptions when a + # defaultdict is a member of a dataclass + + @dataclass + class C: + mp: DefaultDict[str, List] + + + dd = defaultdict(list) + dd["x"].append(12) + c = C(mp=dd) + d = asdict(c) + + assert d == {"mp": {"x": [12]}} + assert d["mp"] is not c.mp # make sure defaultdict is copied + def test_helper_astuple(self): # Basic tests for astuple(), it should return a new tuple. @dataclass |