From 70da0fd8dfb77c39396481f015c28b44a4f32f76 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Wed, 12 Jul 2023 10:03:52 +0530 Subject: [PATCH 1/6] gh-106602: Add __copy__ and __deepcopy__ in Enum --- Lib/enum.py | 6 ++++++ Lib/test/test_enum.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/Lib/enum.py b/Lib/enum.py index 47e31b17c2a495..3e2c17684af2db 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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): + return self + + def __copy__(self): + return self + @property def name(self): """The name of the Enum member.""" diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 54b7d18d454151..80e13b50c17693 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -804,8 +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) + + 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: From 02a583937b518ef8f6e387d5eb3b240d2996c8a4 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Wed, 12 Jul 2023 10:03:52 +0530 Subject: [PATCH 2/6] gh-106602: Add __copy__ and __deepcopy__ in Enum --- Lib/enum.py | 6 ++++++ Lib/test/test_enum.py | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/Lib/enum.py b/Lib/enum.py index 47e31b17c2a495..3e2c17684af2db 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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): + return self + + def __copy__(self): + return self + @property def name(self): """The name of the Enum member.""" diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 54b7d18d454151..8254454cdc6c48 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -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) + 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: From bfd02d621246da255abde3779448d3f4e6cd09e5 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 12 Jul 2023 04:58:47 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-07-12-04-58-45.gh-issue-106602.dGCcXe.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-12-04-58-45.gh-issue-106602.dGCcXe.rst 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 index 00000000000000..d9c122f1d3c723 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-12-04-58-45.gh-issue-106602.dGCcXe.rst @@ -0,0 +1 @@ +Add __copy__ and __deepcopy__ in :mod:`enum` From 326e630ddef8049ef802ec6cb4d10e9ba752352b Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Thu, 13 Jul 2023 01:12:09 +0530 Subject: [PATCH 4/6] Update Lib/test/test_enum.py Co-authored-by: Ethan Furman --- Lib/test/test_enum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 8254454cdc6c48..ede1eb9f392a98 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -807,7 +807,7 @@ def test_copy(self): self.assertIs(copied, TE) deep = copy.deepcopy(TE) self.assertEqual(deep, TE) - self.assertIs(copied, TE) + self.assertIs(deep, TE) def test_copy_member(self): TE = self.MainEnum From 8015aeff9f3f7ef69e5245c90bdf4753abbd260d Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Thu, 13 Jul 2023 01:13:14 +0530 Subject: [PATCH 5/6] Resolve comment --- Lib/enum.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/enum.py b/Lib/enum.py index 3e2c17684af2db..ed98746a96d50c 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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 @@ -1226,11 +1232,6 @@ 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): - return self - - def __copy__(self): - return self @property def name(self): From a5905d407ec5ad77066d504040b585293145fd5e Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 12 Jul 2023 13:06:15 -0700 Subject: [PATCH 6/6] Update Lib/enum.py --- Lib/enum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index ed98746a96d50c..c1dfdbc8a0549f 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1232,7 +1232,6 @@ def __copy__(self): # on class lookup; on instance lookup it either executes a provided function # or raises an AttributeError. - @property def name(self): """The name of the Enum member."""