danielgtaylor / danielgtaylor/python-betterproto
OneOf Enforcement?
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 234
- PR merge metrics
- No merged PRs in 30d
Description
Thanks for all of your work on betterproto, it's been immensely helpful.
What is betterproto's canonical way of enforcing Protobuf `oneof`s are only set to one variant?
From this demo-protobuf:
```proto
syntax = "proto3";
package demo;
message Thing1 {
string name = 1;
}
message Thing2 {
string name = 1;
}
message Thing3 {
string name = 1;
}
message Thing4 {
string name = 1;
}
message OneThing {
oneof t {
Thing1 t1 = 1;
Thing2 t2 = 2;
Thing3 t3 = 3;
Thing4 t4 = 4;
}
}
```
Creating a `OneThing` appears produce default values of non-used `oneof` variants.
```
In [2]: OneThing(t1=Thing1(name='cy'))
Out[2]: OneThing(t1=Thing1(name='cy'), t2=Thing2(name=''), t3=Thing3(name=''), t4=Thing4(name=''))
```
Moreover it'll initialize more than one of them:
```
In [5]: o = OneThing(t1=Thing1(name='cy'), t2=Thing2(name='huh'))
In [6]: o
Out[6]: OneThing(t1=Thing1(name='cy'), t2=Thing2(name='huh'), t3=Thing3(name=''), t4=Thing4(name=''))
```
And that combination passes a round-trip through serialization and de-serialization:
```
In [7]: OneThing.FromString(bytes(o))
Out[7]: OneThing(t1=Thing1(name=''), t2=Thing2(name='huh'), t3=Thing3(name=''), t4=Thing4(name=''))
```
What's the intent as to where this *should* be enforced? Outside of betterproto?
Contributor guide
Assessment
This issue has not been assessed yet.