Skip to content

Commit f027f1f

Browse files
anandoleecopybara-github
authored andcommitted
Breaking change: Remove float_precision from python proto json_format
PiperOrigin-RevId: 826158770
1 parent 50bd022 commit f027f1f

File tree

3 files changed

+0
-35
lines changed

3 files changed

+0
-35
lines changed

python/google/protobuf/internal/json_format_test.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,17 +1005,6 @@ def testParseDoubleToFloat(self):
10051005

10061006
def testFloatPrecision(self):
10071007
message = json_format_proto3_pb2.TestMessage()
1008-
message.float_value = 1.123456789
1009-
# Set to 8 valid digits.
1010-
text = '{\n "floatValue": 1.1234568\n}'
1011-
self.assertEqual(
1012-
json_format.MessageToJson(message, float_precision=8), text
1013-
)
1014-
# Set to 7 valid digits.
1015-
text = '{\n "floatValue": 1.123457\n}'
1016-
self.assertEqual(
1017-
json_format.MessageToJson(message, float_precision=7), text
1018-
)
10191008

10201009
# Default float_precision will automatic print shortest float.
10211010
message.float_value = 1.1000000011

python/google/protobuf/json_format.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import math
2727
from operator import methodcaller
2828
import re
29-
import warnings
3029

3130
from google.protobuf import descriptor
3231
from google.protobuf import message_factory
@@ -85,7 +84,6 @@ def MessageToJson(
8584
sort_keys=False,
8685
use_integers_for_enums=False,
8786
descriptor_pool=None,
88-
float_precision=None,
8987
ensure_ascii=True,
9088
always_print_fields_with_no_presence=False,
9189
):
@@ -107,8 +105,6 @@ def MessageToJson(
107105
use_integers_for_enums: If true, print integers instead of enum names.
108106
descriptor_pool: A Descriptor Pool for resolving types. If None use the
109107
default.
110-
float_precision: Deprecated. If set, use this to specify float field valid
111-
digits.
112108
ensure_ascii: If True, strings with non-ASCII characters are escaped. If
113109
False, Unicode strings are returned unchanged.
114110
@@ -119,7 +115,6 @@ def MessageToJson(
119115
preserving_proto_field_name,
120116
use_integers_for_enums,
121117
descriptor_pool,
122-
float_precision,
123118
always_print_fields_with_no_presence,
124119
)
125120
return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)
@@ -131,7 +126,6 @@ def MessageToDict(
131126
preserving_proto_field_name=False,
132127
use_integers_for_enums=False,
133128
descriptor_pool=None,
134-
float_precision=None,
135129
):
136130
"""Converts protobuf message to a dictionary.
137131
@@ -149,8 +143,6 @@ def MessageToDict(
149143
use_integers_for_enums: If true, print integers instead of enum names.
150144
descriptor_pool: A Descriptor Pool for resolving types. If None use the
151145
default.
152-
float_precision: Deprecated. If set, use this to specify float field valid
153-
digits.
154146
155147
Returns:
156148
A dict representation of the protocol buffer message.
@@ -159,7 +151,6 @@ def MessageToDict(
159151
preserving_proto_field_name,
160152
use_integers_for_enums,
161153
descriptor_pool,
162-
float_precision,
163154
always_print_fields_with_no_presence,
164155
)
165156
# pylint: disable=protected-access
@@ -182,7 +173,6 @@ def __init__(
182173
preserving_proto_field_name=False,
183174
use_integers_for_enums=False,
184175
descriptor_pool=None,
185-
float_precision=None,
186176
always_print_fields_with_no_presence=False,
187177
):
188178
self.always_print_fields_with_no_presence = (
@@ -191,15 +181,6 @@ def __init__(
191181
self.preserving_proto_field_name = preserving_proto_field_name
192182
self.use_integers_for_enums = use_integers_for_enums
193183
self.descriptor_pool = descriptor_pool
194-
if float_precision:
195-
warnings.warn(
196-
'float_precision option is deprecated for json_format. '
197-
'This will turn into error in 7.34.0, please remove it '
198-
'before that.'
199-
)
200-
self.float_format = '.{}g'.format(float_precision)
201-
else:
202-
self.float_format = None
203184

204185
def ToJsonString(self, message, indent, sort_keys, ensure_ascii):
205186
js = self._MessageToJsonObject(message)
@@ -322,8 +303,6 @@ def _FieldToJsonObject(self, field, value):
322303
return _INFINITY
323304
if math.isnan(value):
324305
return _NAN
325-
if self.float_format:
326-
return float(format(value, self.float_format))
327306
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_FLOAT:
328307
return type_checkers.ToShortestFloat(value)
329308

python/google/protobuf/proto_json.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def serialize(
1919
preserving_proto_field_name: bool=False,
2020
use_integers_for_enums: bool=False,
2121
descriptor_pool: Optional[DescriptorPool]=None,
22-
float_precision: int=None,
2322
) -> dict:
2423
"""Converts protobuf message to a dictionary.
2524
@@ -37,7 +36,6 @@ def serialize(
3736
use_integers_for_enums: If true, print integers instead of enum names.
3837
descriptor_pool: A Descriptor Pool for resolving types. If None use the
3938
default.
40-
float_precision: If set, use this to specify float field valid digits.
4139
4240
Returns:
4341
A dict representation of the protocol buffer message.
@@ -47,7 +45,6 @@ def serialize(
4745
always_print_fields_with_no_presence=always_print_fields_with_no_presence,
4846
preserving_proto_field_name=preserving_proto_field_name,
4947
use_integers_for_enums=use_integers_for_enums,
50-
float_precision=float_precision,
5148
)
5249

5350
def parse(

0 commit comments

Comments
 (0)