Visitar URL original
[Enum] add __deepcopy__ method to Enum · Issue #106602 · python/cpython · GitHub
Skip to content

[Enum] add __deepcopy__ method to Enum #106602

Closed

Activity

  1. self-assigned this
    on Jul 10, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jul 10, 2023
  3. Agent-Hellboy commented on Jul 11, 2023

    @Agent-Hellboy
    Contributor

    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

  4. ethanfurman commented on Jul 11, 2023

    @ethanfurman
    MemberAuthor

    [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.

  5. ethanfurman commented on Jul 11, 2023

    @ethanfurman
    MemberAuthor

    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.

  6. added 2 commits that reference this issue on Jul 12, 2023
  7. Agent-Hellboy commented on Jul 12, 2023

    @Agent-Hellboy
    Contributor

    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

  8. added a commit that references this issue on Jul 12, 2023
  9. added 2 commits that reference this issue on Jul 12, 2023
  10. added 2 commits that reference this issue on Jul 12, 2023
  11. ethanfurman commented on Jul 12, 2023

    @ethanfurman
    MemberAuthor

    Thanks, @Agent-Hellboy, for the help!

  12. Agent-Hellboy commented on Jul 13, 2023

    @Agent-Hellboy
    Contributor

    Thanks @ethanfurman, looking forward to contributions like these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions