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 4 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 @@ -1226,6 +1226,12 @@ def __reduce_ex__(self, proto):
# on class lookup; on instance lookup it either executes a provided function
# or raises an AttributeError.

def __deepcopy__(self,memo):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move these two methods above the enum.property comment.

return self

def __copy__(self):
return self

ethanfurman marked this conversation as resolved.
Show resolved Hide resolved
@property
def name(self):
"""The name of the Enum member."""
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(copied, TE)
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved

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`