https://github.com/python/cpython/commit/373036a82c2127da8c54993ec95a966a135ec4c1
commit: 373036a82c2127da8c54993ec95a966a135ec4c1
branch: 3.12
author: Bénédikt Tran <[email protected]>
committer: ericvsmith <[email protected]>
date: 2024-09-27T16:57:30Z
summary:

[3.12] gh-89683: add tests for `deepcopy` on frozen dataclasses (GH-123098) 
(gh-124679)

gh-89683: add tests for `deepcopy` on frozen dataclasses (gh-123098)

Co-authored-by: Eric V. Smith <[email protected]>

files:
M Lib/test/test_dataclasses/__init__.py

diff --git a/Lib/test/test_dataclasses/__init__.py 
b/Lib/test/test_dataclasses/__init__.py
index 94183b002dabb5..8421cf974cf5ab 100644
--- a/Lib/test/test_dataclasses/__init__.py
+++ b/Lib/test/test_dataclasses/__init__.py
@@ -17,6 +17,7 @@
 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, defaultdict
+from copy import deepcopy
 from functools import total_ordering
 
 import typing       # Needed for the string "typing.ClassVar[int]" to work as 
an annotation.
@@ -3071,6 +3072,48 @@ class C:
         with self.assertRaisesRegex(TypeError, 'unhashable type'):
             hash(C({}))
 
+    def test_frozen_deepcopy_without_slots(self):
+        # see: https://github.com/python/cpython/issues/89683
+        @dataclass(frozen=True, slots=False)
+        class C:
+            s: str
+
+        c = C('hello')
+        self.assertEqual(deepcopy(c), c)
+
+    def test_frozen_deepcopy_with_slots(self):
+        # see: https://github.com/python/cpython/issues/89683
+        with self.subTest('generated __slots__'):
+            @dataclass(frozen=True, slots=True)
+            class C:
+                s: str
+
+            c = C('hello')
+            self.assertEqual(deepcopy(c), c)
+
+        with self.subTest('user-defined __slots__ and no __{get,set}state__'):
+            @dataclass(frozen=True, slots=False)
+            class C:
+                __slots__ = ('s',)
+                s: str
+
+            # with user-defined slots, __getstate__ and __setstate__ are not
+            # automatically added, hence the error
+            err = r"^cannot\ assign\ to\ field\ 's'$"
+            self.assertRaisesRegex(FrozenInstanceError, err, deepcopy, C(''))
+
+        with self.subTest('user-defined __slots__ and __{get,set}state__'):
+            @dataclass(frozen=True, slots=False)
+            class C:
+                __slots__ = ('s',)
+                __getstate__ = dataclasses._dataclass_getstate
+                __setstate__ = dataclasses._dataclass_setstate
+
+                s: str
+
+            c = C('hello')
+            self.assertEqual(deepcopy(c), c)
+
 
 class TestSlots(unittest.TestCase):
     def test_simple(self):

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to