Add betterproto.Enum __copy__ and __deepcopy__ implementations (#566)

* Add betterproto.Enum __copy__ and __deepcopy__ implementations

betterproto.Enum is missing __copy__ and __deepcopy__ implementations, which were recently added to enum.Enum, see https://github.com/python/cpython/issues/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>
This commit is contained in:
atomicmac 2024-03-29 07:08:02 -07:00 committed by GitHub
parent 126b256b4c
commit df1ba911b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -156,6 +156,12 @@ class Enum(IntEnum if TYPE_CHECKING else int, metaclass=EnumType):
f"{self.__class__.__name__} Cannot delete a member's attributes." f"{self.__class__.__name__} Cannot delete a member's attributes."
) )
def __copy__(self) -> Self:
return self
def __deepcopy__(self, memo: Any) -> Self:
return self
@classmethod @classmethod
def try_value(cls, value: int = 0) -> Self: def try_value(cls, value: int = 0) -> Self:
"""Return the value which corresponds to the value. """Return the value which corresponds to the value.