Skip to content

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

Open
@priesgo

Description

@priesgo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmediumMedium effort issue, can fit in a single PRmedium priority

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions