Skip to content

deepcopy with betterproto.Enum fields fails #567

Closed
@atomicmac

Description

@atomicmac

Summary

Using deepcopy on a mesage with a betterproto.Enum fields fails

Reproduction Steps

Create a message that has a betterproto.Enum nested in its hierarchy somewhere.
Execute copy.deepcopy on the message.

Expected Results

A deep copy of the message

Actual Results

A TypeError occurs when the Enum's new is invoked by copy._reconstruct

Screenshot from 2024-03-27 11-29-44

/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:153: in deepcopy
    y = copier(memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/site-packages/betterproto/__init__.py:882: in __deepcopy__
    kwargs[name] = deepcopy(value)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:153: in deepcopy
    y = copier(memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/site-packages/betterproto/__init__.py:882: in __deepcopy__
    kwargs[name] = deepcopy(value)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:146: in deepcopy
    y = copier(x, memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:206: in _deepcopy_list
    append(deepcopy(a, memo))
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:153: in deepcopy
    y = copier(memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/site-packages/betterproto/__init__.py:882: in __deepcopy__
    kwargs[name] = deepcopy(value)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:153: in deepcopy
    y = copier(memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/site-packages/betterproto/__init__.py:882: in __deepcopy__
    kwargs[name] = deepcopy(value)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:153: in deepcopy
    y = copier(memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/site-packages/betterproto/__init__.py:882: in __deepcopy__
    kwargs[name] = deepcopy(value)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:153: in deepcopy
    y = copier(memo)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/site-packages/betterproto/__init__.py:882: in __deepcopy__
    kwargs[name] = deepcopy(value)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:172: in deepcopy
    y = _reconstruct(x, memo, *rv)
/home/user/.pyenv/versions/3.11.4/lib/python3.11/copy.py:265: in _reconstruct
    y = func(*args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <enum 'PixelFormat'>, args = (1,)

    def __newobj__(cls, *args):
>       return cls.__new__(cls, *args)
E       TypeError: Enum.__new__() takes 1 positional argument but 2 were given

/home/user/.pyenv/versions/3.11.4/lib/python3.11/copyreg.py:105: TypeError

I thought this error could be the result of python/cpython#106602 but I upgraded to 3.11.8 and see the same behavior. It looks like betterproto.Enum misses out on the enum.Enum.deepcopy and copy methods because it does not inherit directly from enum.Enum

System Information

Python 3.11.8
Name: betterproto
Version: 2.0.0b6
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: danielgtaylor@gmail.com
License: MIT
Location: /home/user/.pyenv/versions/3.11.8/envs/ampcv-3.11.8/lib/python3.11/site-packages
Requires: grpclib, python-dateutil, typing-extensions

Checklist

  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions