danielgtaylor / python-betterproto

Clean, modern, Python 3.6+ code generator & library for Protobuf 3 and async gRPC
MIT License
1.56k stars 218 forks source link

Remove error when no fields are set in oneof #612

Closed AdrienVannson closed 1 month ago

AdrienVannson commented 2 months ago

Summary

Fix https://github.com/danielgtaylor/python-betterproto/issues/604

Methods such as parse are often called on a new object: value = cls().parse(value). However, is the message contains oneof fields, this used to make the validator _validate_field_groups fail when using pydantic.

I removed the check that at least one value of the oneof is defined. This is coherent with gRPC's documentation: "If you have a message with many fields and where at most one field will be set at the same time, you can enforce this behavior and save memory by using the oneof feature." and "You can check which value in a oneof is set (if any) using a special case() or WhichOneof() method"

Checklist

AdrienVannson commented 2 months ago

I forget to mention: this problem makes some of the tests from test_inputs.py fail. The problem only appears when plugin_output_package is manually set to "tests.output_betterproto_pydantic", which is not the case by default. Once this problem (and at least another one) is fixed, I will send a PR to also execute the input tests with Pydantic.

Gobot1234 commented 1 month ago

I forget to mention: this problem makes some of the tests from test_inputs.py fail. The problem only appears when plugin_output_package is manually set to "tests.output_betterproto_pydantic", which is not the case by default. Once this problem (and at least another one) is fixed, I will send a PR to also execute the input tests with Pydantic.

That would be a huge help actually

haibo-looki commented 2 weeks ago

Need this fix. Hoping it can be released in the near future.

AdrienVannson commented 2 weeks ago

I would like to fix a few small problems before, but it should not take long. Once this is merged, I think we can release a new version. In the meantime, you can use the last version on master, which should work fine.