danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enums are set as integers instead of enums when parsing betterproto.Message

ZivMBS opened this issue · comments

Summary

Trying to parse a message with an enum field sets its value to an integer instead of an instance of the enum

Reproduction Steps

Create a betterproto.Message that has an enum field and parse it.
The given field value will be the actual number instead of the enum class instance.

from dataclasses import dataclass
import betterproto

class ExampleEnum(betterproto.Enum):
    A = 1
    B = 2

@dataclass(eq=False, repr=False)
class ExampleMessage(betterproto.Message):
    example_enum: "ExampleEnum" = betterproto.enum_field(1)

Expected Results

In [5]: ExampleMessage().parse(b'\x08\01')
Out[5]: ExampleMessage(example_enum=<ExampleEnum.A: 1>)

In [6]: ExampleMessage().parse(b'\x08\02')
Out[6]: ExampleMessage(example_enum=<ExampleEnum.B: 2>)

Actual Results

In [5]: ExampleMessage().parse(b'\x08\01')
Out[5]: ExampleMessage(example_enum=1)

In [6]: ExampleMessage().parse(b'\x08\02')
Out[6]: ExampleMessage(example_enum=2)

System Information

libprotoc 23.3
Python 3.9.5
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/zivmbs/.local/share/virtualenvs/chess_server-Lj65NqB0/lib/python3.9/site-packages
Requires: grpclib, python-dateutil
Required-by:

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.

This has already been fixed on main

@Gobot1234 if this is fixed on main, could be this be added to v2 too?

currently on v2 this behaviour makes one to write boilerplate code in order to fix the issue

op suggested a good example, which basically makes default constructor unusable. one have to define inner fields manually so the whole class would behave as expected

just checked this on version 1.2.5 and issue persists there

image