|
12 | 12 | import weakref |
13 | 13 | import unittest |
14 | 14 | from unittest.mock import Mock |
15 | | -from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol |
| 15 | +from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict |
16 | 16 | from typing import get_type_hints |
17 | | -from collections import deque, OrderedDict, namedtuple |
| 17 | +from collections import deque, OrderedDict, namedtuple, defaultdict |
18 | 18 | from functools import total_ordering |
19 | 19 |
|
20 | 20 | import typing # Needed for the string "typing.ClassVar[int]" to work as an annotation. |
@@ -1677,6 +1677,23 @@ class C: |
1677 | 1677 | self.assertIsNot(d['f'], t) |
1678 | 1678 | self.assertEqual(d['f'].my_a(), 6) |
1679 | 1679 |
|
| 1680 | + def test_helper_asdict_defaultdict(self): |
| 1681 | + # Ensure asdict() does not throw exceptions when a |
| 1682 | + # defaultdict is a member of a dataclass |
| 1683 | + |
| 1684 | + @dataclass |
| 1685 | + class C: |
| 1686 | + mp: DefaultDict[str, List] |
| 1687 | + |
| 1688 | + |
| 1689 | + dd = defaultdict(list) |
| 1690 | + dd["x"].append(12) |
| 1691 | + c = C(mp=dd) |
| 1692 | + d = asdict(c) |
| 1693 | + |
| 1694 | + assert d == {"mp": {"x": [12]}} |
| 1695 | + assert d["mp"] is not c.mp # make sure defaultdict is copied |
| 1696 | + |
1680 | 1697 | def test_helper_astuple(self): |
1681 | 1698 | # Basic tests for astuple(), it should return a new tuple. |
1682 | 1699 | @dataclass |
|
0 commit comments