fix: remove float_precision for protobuf 7 (#559) · googleapis/proto-plus-python@390b9d5

@@ -36,6 +36,9 @@

36363737

PROTOBUF_VERSION = google.protobuf.__version__

383839+

# extract the major version code

40+

_PROTOBUF_MAJOR_VERSION = PROTOBUF_VERSION.partition(".")[0]

41+3942

_upb = has_upb() # Important to cache result here.

40434144

@@ -383,7 +386,7 @@ def _warn_if_including_default_value_fields_is_used_protobuf_5(

383386

including_default_value_fields (Optional(bool)): The value of `including_default_value_fields` set by the user.

384387

"""

385388

if (

386-

PROTOBUF_VERSION[0] not in ("3", "4")

389+

_PROTOBUF_MAJOR_VERSION not in ("3", "4")

387390

and including_default_value_fields is not None

388391

):

389392

warnings.warn(

@@ -491,7 +494,7 @@ def to_json(

491494

An indent level of 0 or negative will only insert newlines.

492495

Pass None for the most compact representation without newlines.

493496

float_precision (Optional(int)): If set, use this to specify float field valid digits.

494-

Default is None.

497+

Default is None. [DEPRECATED] float_precision was removed in Protobuf 7.x.

495498

always_print_fields_with_no_presence (Optional(bool)): If True, fields without

496499

presence (implicit presence scalars, repeated fields, and map fields) will

497500

always be serialized. Any field that supports presence is not affected by

@@ -501,36 +504,7 @@ def to_json(

501504

Returns:

502505

str: The json string representation of the protocol buffer.

503506

"""

504-505-

print_fields = cls._normalize_print_fields_without_presence(

506-

always_print_fields_with_no_presence, including_default_value_fields

507-

)

508-509-

if PROTOBUF_VERSION[0] in ("3", "4"):

510-

return MessageToJson(

511-

cls.pb(instance),

512-

use_integers_for_enums=use_integers_for_enums,

513-

including_default_value_fields=print_fields,

514-

preserving_proto_field_name=preserving_proto_field_name,

515-

sort_keys=sort_keys,

516-

indent=indent,

517-

float_precision=float_precision,

518-

)

519-

else:

520-

# The `including_default_value_fields` argument was removed from protobuf 5.x

521-

# and replaced with `always_print_fields_with_no_presence` which very similar but has

522-

# handles optional fields consistently by not affecting them.

523-

# The old flag accidentally had inconsistent behavior between proto2

524-

# optional and proto3 optional fields.

525-

return MessageToJson(

526-

cls.pb(instance),

527-

use_integers_for_enums=use_integers_for_enums,

528-

always_print_fields_with_no_presence=print_fields,

529-

preserving_proto_field_name=preserving_proto_field_name,

530-

sort_keys=sort_keys,

531-

indent=indent,

532-

float_precision=float_precision,

533-

)

507+

return _message_to_map(map_fn=MessageToJson, **locals())

534508535509

def from_json(cls, payload, *, ignore_unknown_fields=False) -> "Message":

536510

"""Given a json string representing an instance,

@@ -576,7 +550,7 @@ def to_dict(

576550

This value must match `always_print_fields_with_no_presence`,

577551

if both arguments are explicitly set.

578552

float_precision (Optional(int)): If set, use this to specify float field valid digits.

579-

Default is None.

553+

Default is None. [DEPRECATED] float_precision was removed in Protobuf 7.x.

580554

always_print_fields_with_no_presence (Optional(bool)): If True, fields without

581555

presence (implicit presence scalars, repeated fields, and map fields) will

582556

always be serialized. Any field that supports presence is not affected by

@@ -588,32 +562,7 @@ def to_dict(

588562

Messages and map fields are represented as dicts,

589563

repeated fields are represented as lists.

590564

"""

591-592-

print_fields = cls._normalize_print_fields_without_presence(

593-

always_print_fields_with_no_presence, including_default_value_fields

594-

)

595-596-

if PROTOBUF_VERSION[0] in ("3", "4"):

597-

return MessageToDict(

598-

cls.pb(instance),

599-

including_default_value_fields=print_fields,

600-

preserving_proto_field_name=preserving_proto_field_name,

601-

use_integers_for_enums=use_integers_for_enums,

602-

float_precision=float_precision,

603-

)

604-

else:

605-

# The `including_default_value_fields` argument was removed from protobuf 5.x

606-

# and replaced with `always_print_fields_with_no_presence` which very similar but has

607-

# handles optional fields consistently by not affecting them.

608-

# The old flag accidentally had inconsistent behavior between proto2

609-

# optional and proto3 optional fields.

610-

return MessageToDict(

611-

cls.pb(instance),

612-

always_print_fields_with_no_presence=print_fields,

613-

preserving_proto_field_name=preserving_proto_field_name,

614-

use_integers_for_enums=use_integers_for_enums,

615-

float_precision=float_precision,

616-

)

565+

return _message_to_map(map_fn=MessageToDict, **locals())

617566618567

def copy_from(cls, instance, other):

619568

"""Equivalent for protobuf.Message.CopyFrom

@@ -966,4 +915,48 @@ def pb(self) -> Type[message.Message]:

966915

return self._pb

967916968917918+

def _message_to_map(

919+

cls,

920+

map_fn,

921+

instance,

922+

*,

923+

including_default_value_fields=None,

924+

always_print_fields_with_no_presence=None,

925+

float_precision=None,

926+

**kwargs,

927+

):

928+

"""

929+

Helper for logic for Message.to_dict and Message.to_json

930+

"""

931+932+

# The `including_default_value_fields` argument was removed from protobuf 5.x

933+

# and replaced with `always_print_fields_with_no_presence` which is similar but

934+

# handles optional fields consistently by not affecting them.

935+

# The old flag accidentally had inconsistent behavior between optional fields

936+

# in proto2 and proto3.

937+

print_fields = cls._normalize_print_fields_without_presence(

938+

always_print_fields_with_no_presence, including_default_value_fields

939+

)

940+

if _PROTOBUF_MAJOR_VERSION in ("3", "4"):

941+

kwargs["including_default_value_fields"] = print_fields

942+

else:

943+

kwargs["always_print_fields_with_no_presence"] = print_fields

944+945+

if float_precision:

946+

# float_precision removed in protobuf 7

947+

if _PROTOBUF_MAJOR_VERSION in ("3", "4", "5", "6"):

948+

kwargs["float_precision"] = float_precision

949+

warning_msg = "`float_precision` will be removed in Protobuf 7.x."

950+951+

else: # pragma: NO COVER

952+

warning_msg = (

953+

"`float_precision` was removed in Protobuf 7.x+, and will be ignored."

954+

)

955+

warnings.warn(warning_msg, DeprecationWarning, stacklevel=3)

956+

# suppress similar float_precision warning from protobuf library.

957+

with warnings.catch_warnings():

958+

warnings.filterwarnings("ignore", message=".*float_precision.*")

959+

return map_fn(cls.pb(instance), **kwargs)

960+961+969962

__all__ = ("Message",)