danielgtaylor / danielgtaylor/python-betterproto

to_dict() fails to write boolean fields without default when value is False

Offen
#95 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement medium medium priority
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
234
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

When you have a message with a boolean value as follows:
```
message Patient {
bool isRnaAvailable = 1;
}
```
then an object like
```
patient = Patient()
patient.is_rna_available = np.random.choice([True, False], 1)[0]
```
fails to write the `is_rna_available` field when it is False when calling `patient.to_dict()`, this does not happen when the value is True.

The issue seems to be in line 824 of file `betterproto/__init__py` where it runs the following comparison `elif v != self._get_field_default(field, meta) or include_default_values:`. It should be something like 'elif v != None or include_default_values', otherwise when you set a field to the same value as the default value and do not set `include_default values` then it is not written. This may be also the case for other types.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start in betterproto/__init__.py at the to_dict() logic around line 824 and reproduce the Patient example with is_rna_available set to False. Check how fields equal to their defaults are handled, including other field types, and verify that the resulting dictionary preserves explicitly assigned false values without requiring include_default_values.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
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.