]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106666)
authorPrince Roshan <princekrroshan01@gmail.com>
Wed, 12 Jul 2023 21:01:17 +0000 (02:31 +0530)
committerGitHub <noreply@github.com>
Wed, 12 Jul 2023 21:01:17 +0000 (14:01 -0700)
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2023-07-12-04-58-45.gh-issue-106602.dGCcXe.rst [new file with mode: 0644]

index 202f0da028bdfe33d553a206b422944c78abf422..0c985b2c778569c07a9ea91d11eb48559fc8d97a 100644 (file)
@@ -1218,6 +1218,12 @@ class Enum(metaclass=EnumType):
     def __reduce_ex__(self, proto):
         return self.__class__, (self._value_, )
 
+    def __deepcopy__(self,memo):
+        return self
+
+    def __copy__(self):
+        return self
+
     # enum.property is used to provide access to the `name` and
     # `value` attributes of enum members while keeping some measure of
     # protection from modification, while still allowing for an enumeration
index adb1e0e52c5485bf1d49e95380e4a7558dfe3f8b..a286411f7bcf575552d1ab513dae11410fd6df41 100644 (file)
@@ -804,9 +804,17 @@ class _MinimalOutputTests:
         TE = self.MainEnum
         copied = copy.copy(TE)
         self.assertEqual(copied, TE)
+        self.assertIs(copied, TE)
         deep = copy.deepcopy(TE)
         self.assertEqual(deep, TE)
+        self.assertIs(deep, TE)
 
+    def test_copy_member(self):
+        TE = self.MainEnum
+        copied = copy.copy(TE.first)
+        self.assertIs(copied, TE.first)
+        deep = copy.deepcopy(TE.first)
+        self.assertIs(deep, TE.first)
 
 class _FlagTests:
 
diff --git a/Misc/NEWS.d/next/Library/2023-07-12-04-58-45.gh-issue-106602.dGCcXe.rst b/Misc/NEWS.d/next/Library/2023-07-12-04-58-45.gh-issue-106602.dGCcXe.rst
new file mode 100644 (file)
index 0000000..d9c122f
--- /dev/null
@@ -0,0 +1 @@
+Add __copy__ and __deepcopy__ in :mod:`enum`