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

[Enum] add __deepcopy__ method to Enum #106602

Closed
ethanfurman opened this issue Jul 10, 2023 · 6 comments
Closed

[Enum] add __deepcopy__ method to Enum #106602

ethanfurman opened this issue Jul 10, 2023 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ethanfurman
Copy link
Member

ethanfurman commented Jul 10, 2023

@ethanfurman ethanfurman added the type-bug An unexpected behavior, bug, or error label Jul 10, 2023
@ethanfurman ethanfurman self-assigned this Jul 10, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Jul 10, 2023
@Agent-Hellboy
Copy link
Contributor

Agent-Hellboy commented Jul 11, 2023

Hi @ethanfurman, I thought of implementing this.

did you mean we should be able to make a deep copy of the whole enum like adding is check in here
https://github.com/python/cpython/blob/1f2921b72c369b19c2e32aaedb9f8c63e0cb8b48/Lib/test/test_enum.py#L803C1-L808C35

if yes, then we need to make changes in copy module because Enums are getting created using a custom metaclass
and deep copy returns the object as it is if the class is a subclass of a type
https://github.com/python/cpython/blob/1f2921b72c369b19c2e32aaedb9f8c63e0cb8b48/Lib/copy.py#L137C1-L139C42

Where did the bug get reported? Do you know if I can see the StackOverflow link?

if it's about copying a member (adding deepcopy in Enum) then I will explore more

    def __deepcopy__(self, memo):
        new_enum = self.__class__(self.value)

currently, it is taking the same object for new_enum as well as self

@ethanfurman
Copy link
Member Author

[question about is check]

Yes, those should both be is checks -- enum members are singletons, so a copy of any kind should return the exact same object.

[copy module questions]

An enum class, such as Color, is processed by that branch; a member, such as Color.RED, falls through to the else branch where a __deepcopy__ is looked for.

Enum.__copy__ and Enum.__deepcopy__ just need to return self.

[bug report question]

This issue is where the bug was reported.

@ethanfurman
Copy link
Member Author

The current test_copy is testing an enum class -- we need to add a test for enum members. All those copy tests should be using is.

Agent-Hellboy added a commit to Agent-Hellboy/cpython that referenced this issue Jul 12, 2023
Agent-Hellboy added a commit to Agent-Hellboy/cpython that referenced this issue Jul 12, 2023
@Agent-Hellboy
Copy link
Contributor

Agent-Hellboy commented Jul 12, 2023

This issue is where the bug was reported.

Ohh, I thought, this one also got reported on StackOverflow just like a calendar module feature I once picked.

I have added a PR
Btw, I learned a lot about Enum's internal design because of the wrong assumption that deep-copy(Enum or its member) will return a new Enum

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 12, 2023
(cherry picked from commit 357e9e9)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 12, 2023
(cherry picked from commit 357e9e9)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
ethanfurman pushed a commit that referenced this issue Jul 12, 2023
gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106666)
(cherry picked from commit 357e9e9)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
ethanfurman pushed a commit that referenced this issue Jul 12, 2023
gh-106602: [Enum] Add __copy__ and __deepcopy__ (GH-106666)
(cherry picked from commit 357e9e9)

Co-authored-by: Prince Roshan <princekrroshan01@gmail.com>
@ethanfurman
Copy link
Member Author

Thanks, @Agent-Hellboy, for the help!

@Agent-Hellboy
Copy link
Contributor

Thanks @ethanfurman, looking forward to contributions like these.

atomicmac added a commit to atomicmac/python-betterproto that referenced this issue Mar 27, 2024
betterproto.Enum is missing __copy__ and __deepcopy__ implementations, which were recently added to enum.Enum, see python/cpython#106602
This fixes the bug where betterproto messages with Enums nested within cannot be copied via copy.deepcopy.
Gobot1234 added a commit to danielgtaylor/python-betterproto that referenced this issue Mar 29, 2024
* Add betterproto.Enum __copy__ and __deepcopy__ implementations

betterproto.Enum is missing __copy__ and __deepcopy__ implementations, which were recently added to enum.Enum, see python/cpython#106602
This fixes the bug where betterproto messages with Enums nested within cannot be copied via copy.deepcopy.

* Type hint on Enum.__copy__

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>

* Type hint on Enum.__deepcopy__

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>

---------

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
ClausHolbechArista pushed a commit to ClausHolbechArista/python-aristaproto that referenced this issue Apr 29, 2024
* Add betterproto.Enum __copy__ and __deepcopy__ implementations

betterproto.Enum is missing __copy__ and __deepcopy__ implementations, which were recently added to enum.Enum, see python/cpython#106602
This fixes the bug where betterproto messages with Enums nested within cannot be copied via copy.deepcopy.

* Type hint on Enum.__copy__

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>

* Type hint on Enum.__deepcopy__

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>

---------

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants