Skip to content

Commit 77d9fdf

Browse files
[3.12] gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106695)
gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106666) (cherry picked from commit 357e9e9) Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
1 parent ec7b05a commit 77d9fdf

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/enum.py

+6
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,12 @@ def __hash__(self):
12421242
def __reduce_ex__(self, proto):
12431243
return self.__class__, (self._value_, )
12441244

1245+
def __deepcopy__(self,memo):
1246+
return self
1247+
1248+
def __copy__(self):
1249+
return self
1250+
12451251
# enum.property is used to provide access to the `name` and
12461252
# `value` attributes of enum members while keeping some measure of
12471253
# protection from modification, while still allowing for an enumeration

Lib/test/test_enum.py

+8
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,17 @@ def test_copy(self):
804804
TE = self.MainEnum
805805
copied = copy.copy(TE)
806806
self.assertEqual(copied, TE)
807+
self.assertIs(copied, TE)
807808
deep = copy.deepcopy(TE)
808809
self.assertEqual(deep, TE)
810+
self.assertIs(deep, TE)
809811

812+
def test_copy_member(self):
813+
TE = self.MainEnum
814+
copied = copy.copy(TE.first)
815+
self.assertIs(copied, TE.first)
816+
deep = copy.deepcopy(TE.first)
817+
self.assertIs(deep, TE.first)
810818

811819
class _FlagTests:
812820

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add __copy__ and __deepcopy__ in :mod:`enum`

0 commit comments

Comments
 (0)