danielgtaylor / danielgtaylor/python-betterproto

Implicit optional is not respected

Abierto
#645 3 comentarios 0 reacciones 0 asignados Ver en GitHub
bug investigation needed
Lenguaje dominante
Python
Estrellas
1.8k
Forks
234
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

### 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.

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Reproduce el problema con el Sample proto mostrado y la dataclass de Python generada; después, sigue cómo la declaración del campo y la serialización de bytes(Sample(foo=False)) gestionan los optional fields implícitos frente a los explícitos. Se considera terminado cuando los fields implícitos serializan los valores predeterminados de forma coherente con los optional fields explícitos y coinciden con el comportamiento documentado de protobuf.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.