Closed AdrienVannson closed 1 month ago
The parse methods used to be hybridmethods not sure why this code has disappeared
I don't think so, if I do a git blame, I see that the function was last updated here: https://github.com/danielgtaylor/python-betterproto/commit/8659c511
This commit creates the parse function directly as a standard method. However, from_dict
is hybrid.
Summary
Decoding a message with oneof fails.
Reproduction Steps
To show the problem, here is a simple example. Compile the following proto messages:
In a Python script, creating an instance without any parameter (
Choice()
) fails with a validation error:Value error, Group choice has no value; all fields are None
.This error comes from
_validate_field_groups
insrc/betterproto/__init__.py
.Expected Results
In this simple case, the error might seem expected. However, betterproto creates instances of messages without any parameter a bit everywhere in the code. For example, parsing a message is done as follows:
value = cls().parse(value)
. An instance is created without any parameter, which fails if the message contains a oneof field.Actual Results
I would have expected receiving messages with oneof fields to work.
I see two potential solutions:
parse
,from_dict
, etc should probably be class methods instead. It would probably need many changes in the source code, and would also need an update to the rust codecparse
have no reason to be called on an actual instance of the object (they are always called on an instance created just for the call).What do you think?
System Information
libprotoc 3.12.4 Python 3.12.5 Name: betterproto Version: 2.0.0b7 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: XXX Requires: grpclib, python-dateutil, typing-extensions Required-by: pygrpc-poc
Checklist
pip install -U --pre betterproto
, if possible.