Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106602: Add __copy__ and __deepcopy__ in Enum #106666

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,12 @@ def __hash__(self):
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
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,17 @@ def test_copy(self):
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:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add __copy__ and __deepcopy__ in :mod:`enum`