8000 gh-89683: add tests for `deepcopy` on frozen dataclasses (gh-123098) · python/cpython@5e7eba0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e7eba0

Browse files
picnixzericvsmith
andauthored
gh-89683: add tests for deepcopy on frozen dataclasses (gh-123098)
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
1 parent 2c10832 commit 5e7eba0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Lib/test/test_dataclasses/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
1818
from typing import get_type_hints
1919
from collections import deque, OrderedDict, namedtuple, defaultdict
20+
from copy import deepcopy
2021
from functools import total_ordering, wraps
2122

2223
import typing # Needed for the string "typing.ClassVar[int]" to work as an annotation.
@@ -3175,6 +3176,48 @@ class C:
31753176
with self.assertRaisesRegex(TypeError, 'unhashable type'):
31763177
hash(C({}))
31773178

3179+
def test_frozen_deepcopy_without_slots(self):
3180+
# see: https://github.com/python/cpython/issues/89683
3181+
@dataclass(frozen=True, slots=False)
3182+
class C:
3183+
s: str
3184+
3185+
c = C('hello')
3186+
self.assertEqual(deepcopy(c), c)
3187+
3188+
def test_frozen_deepcopy_with_slots(self):
3189+
# see: https://github.com/python/cpython/issues/89683
3190+
with self.subTest('generated __slots__'):
3191+
@dataclass(frozen=True, slots=True)
3192+
class C:
3193+
s: str
3194+
3195+
c = C('hello')
3196+
self.assertEqual(deepcopy(c), c)
3197+
3198+
with self.subTest('user-defined __slots__ and no __{get,set}state__'):
3199+
@dataclass(frozen=True, slots=False)
3200+
class C:
3201+
__slots__ = ('s',)
3202+
s: str
3203+
3204+
# with user-defined slots, __getstate__ and __setstate__ are not
3205+
# automatically added, hence the error
3206+
err = r"^cannot\ assign\ to\ field\ 's'$"
3207+
self.assertRaisesRegex(FrozenInstanceError, err, deepcopy, C(''))
3208+
3209+
with self.subTest('user-defined __slots__ and __{get,set}state__'):
3210+
@dataclass(frozen=True, slots=False)
3211+
class C:
3212+
__slots__ = ('s',)
3213+
__getstate__ = dataclasses._dataclass_getstate
3214+
__setstate__ = dataclasses._dataclass_setstate
3215+
3216+
s: str
3217+
3218+
c = C('hello')
3219+
self.assertEqual(deepcopy(c), c)
3220+
31783221

31793222
class TestSlots(unittest.TestCase):
31803223
def test_simple(self):

0 commit comments

Comments
 (0)
0