Skip to content

Python protobuf should refuse to serialize Infinity and NaN #11259

@ikonst

Description

@ikonst

Since it doesn't roundtrip, we should refuse to serialize them, per docs:

Note that attempting to serialize NaN or Infinity results in error. (We can't serialize these as string "NaN" or "Infinity" values like we do for regular fields, because they would parse as string_value, not number_value).

Trying with protobuf 4.21.11:

import math

from google.protobuf import json_format, struct_pb2

inf_val = struct_pb2.Value(number_value=math.inf)
inf_json = json_format.MessageToJson(inf_val)
inf_val_roundtrip = json_format.Parse(inf_json, struct_pb2.Value())

print(inf_val)
print(inf_val_roundtrip)

outputs

number_value: inf
string_value: "Infinity"

See also golang/protobuf#1182

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions