Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added tests for asdict() with defaultdict
  • Loading branch information
kwsp committed May 4, 2022
commit 3fe88733eec07f82630d274a8af3fe36d4b34c58
21 changes: 19 additions & 2 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import types
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.
Expand Down Expand Up @@ -1650,6 +1650,23 @@ class C:
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
Expand Down