Serialize negative zero floats instead of dropping them#226
Merged
AdrienVannson merged 2 commits intoJun 14, 2026
Conversation
A float or double field set to -0.0 was dropped from the binary wire format and from to_dict/to_json output: -0.0 compares equal to the 0.0 default and is falsy, so the default checks treated it as unset. The reference protobuf implementation serializes -0.0 (its sign bit is set, distinguishing it from the +0.0 default), so betterproto2 producers were silently losing the sign for any consumer. Add an _is_negative_zero helper and keep the value in all three serialization paths, while still omitting the real +0.0 default.
AdrienVannson
left a comment
Contributor
There was a problem hiding this comment.
Thank you!
I see one line to change to match generated code (I don't think there is any reason to have something different here), other than that it looks great
|
|
||
|
|
||
| def _make_cls(proto_type: str): | ||
| @dataclass(eq=True) |
Contributor
There was a problem hiding this comment.
Suggested change
| @dataclass(eq=True) | |
| @dataclass(eq=False, repr=False) |
To match with standard generated messages
Contributor
Author
There was a problem hiding this comment.
Done in 59614e5 — switched to @dataclass(eq=False, repr=False) to match the generated-message options. Tests still green.
Use @DataClass(eq=False, repr=False) on the test Message, matching the options betterproto2 emits for generated messages, per review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A
floatordoublefield set to-0.0is silently dropped during serialization.-0.0compares equal to the0.0field default and is falsy, so the "is this the default value?" checks treat it as unset and skip it.The reference protobuf implementation keeps
-0.0: its sign bit is set, which distinguishes it from the default+0.0.So a betterproto2 producer loses the sign for any consumer (including another betterproto2 process), while
+0.0is correctly omitted. Decoding already worked: parsing the reference-0.0wire yields-0.0, so only the encode side was affected.Fix
Add a small
_is_negative_zerohelper and treat-0.0as a non-default value in the three serialization paths that relied onvalue == default/not bool(value):__bytes__to_dict/to_jsonPROTO_JSON outputto_dictPYTHON outputThe real
+0.0default keeps being omitted everywhere.Tests
tests/test_negative_zero.pychecks that-0.0survives a binary round trip and a dict/json round trip for bothfloatanddouble, and that+0.0is still omitted. The fixed binary output now matches the google reference byte for byte (090000000000000080).Notes
I found this with a differential test of betterproto2 against the google
protobufpackage. AI assistance was used under my direction; I verified the behavior against the reference implementation and reviewed the change.