danielgtaylor / danielgtaylor/python-betterproto

Implicit optional is not respected

Offen
#645 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug investigation needed
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
234
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

### Summary

When you don't add an explicit `optional`, fields are not serialized properly if they happen to be set to the default value.

### Reproduction Steps

```
message Sample {
bool foo = 1
}
```
generates
```
[
{
"name": "foo",
"type": "BOOLEAN",
"mode": "NULLABLE",
}
]
```
but the python code generated is
```
@dataclass(eq=False, repr=False)
class Sample(betterproto.Message):
foo: bool = betterproto.string_field(1)
```
which implies that the boolean is non-nullable.

Then, if we do
```
bytes(Sample(foo=False))
```
it will be serialized to
```
b''
```
and actually treated as null.

If we change the proto to
```
message Sample {
optional bool foo = 1
}
```
then the correct code is generated and the message is serialized as expected.

### Actual Results

The protobuf documentation suggests that not setting `optional` should be treated in the same way as setting it.

### System Information

libprotoc 28.3
Python 3.10.11
betterproto 2.0.0b7

### Checklist

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Reproduziere das Problem mit dem gezeigten Sample proto und der generierten Python dataclass und verfolge anschließend, wie die Felddeklaration und die Serialisierung von bytes(Sample(foo=False)) implizite gegenüber expliziten optional fields behandeln. Als erledigt gilt die Aufgabe, wenn implizite fields Standardwerte konsistent mit expliziten optional fields serialisieren und dem dokumentierten protobuf-Verhalten entsprechen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.