@@ -457,46 +457,6 @@ def _normalize_print_fields_without_presence(
457457 or including_default_value_fields
458458 )
459459
460- @staticmethod
461- def _to_map (
462- cls ,
463- map_fn ,
464- instance ,
465- * ,
466- including_default_value_fields = None ,
467- float_precision = None ,
468- always_print_fields_with_no_presence = None ,
469- ** kwargs ,
470- ):
471- """
472- Helper for logic for to_dict and to_json
473- """
474- print_fields = cls ._normalize_print_fields_without_presence (
475- always_print_fields_with_no_presence , including_default_value_fields
476- )
477-
478- # The `including_default_value_fields` argument was removed from protobuf 5.x
479- # and replaced with `always_print_fields_with_no_presence` which very similar but has
480- # handles optional fields consistently by not affecting them.
481- # The old flag accidentally had inconsistent behavior between proto2
482- # optional and proto3 optional fields.
483- if PROTOBUF_VERSION [0 ] in ("3" , "4" ):
484- kwargs ["including_default_value_fields" ] = print_fields
485- else :
486- kwargs ["always_print_fields_with_no_presence" ] = print_fields
487-
488- if float_precision :
489- # float_precision removed in protobuf 7
490- if int (PROTOBUF_VERSION [0 ]) < 7 :
491- kwargs ["float_precision" ] = float_precision
492- else :
493- warnings .warn (
494- "The argument `float_precision` has been removed from Protobuf 7.x." ,
495- DeprecationWarning ,
496- )
497-
498- return map_fn (cls .pb (instance ), ** kwargs )
499-
500460 def to_json (
501461 cls ,
502462 instance ,
@@ -541,7 +501,7 @@ def to_json(
541501 Returns:
542502 str: The json string representation of the protocol buffer.
543503 """
544- return cls . _to_map (map_fn = MessageToJson , ** locals ())
504+ return _message_to_map (map_fn = MessageToJson , ** locals ())
545505
546506 def from_json (cls , payload , * , ignore_unknown_fields = False ) -> "Message" :
547507 """Given a json string representing an instance,
@@ -599,7 +559,7 @@ def to_dict(
599559 Messages and map fields are represented as dicts,
600560 repeated fields are represented as lists.
601561 """
602- return cls . _to_map (map_fn = MessageToDict , ** locals ())
562+ return _message_to_map (map_fn = MessageToDict , ** locals ())
603563
604564 def copy_from (cls , instance , other ):
605565 """Equivalent for protobuf.Message.CopyFrom
@@ -952,4 +912,44 @@ def pb(self) -> Type[message.Message]:
952912 return self ._pb
953913
954914
915+ def _message_to_map (
916+ cls ,
917+ map_fn ,
918+ instance ,
919+ * ,
920+ including_default_value_fields = None ,
921+ float_precision = None ,
922+ always_print_fields_with_no_presence = None ,
923+ ** kwargs ,
924+ ):
925+ """
926+ Helper for logic for Message.to_dict and Message.to_json
927+ """
928+ print_fields = cls ._normalize_print_fields_without_presence (
929+ always_print_fields_with_no_presence , including_default_value_fields
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 very similar but has
934+ # handles optional fields consistently by not affecting them.
935+ # The old flag accidentally had inconsistent behavior between proto2
936+ # optional and proto3 optional fields.
937+ if PROTOBUF_VERSION [0 ] in ("3" , "4" ):
938+ kwargs ["including_default_value_fields" ] = print_fields
939+ else :
940+ kwargs ["always_print_fields_with_no_presence" ] = print_fields
941+
942+ if float_precision :
943+ # float_precision removed in protobuf 7
944+ if int (PROTOBUF_VERSION [0 ]) < 7 :
945+ kwargs ["float_precision" ] = float_precision
946+ else :
947+ warnings .warn (
948+ "The argument `float_precision` has been removed from Protobuf 7.x." ,
949+ DeprecationWarning ,
950+ )
951+
952+ return map_fn (cls .pb (instance ), ** kwargs )
953+
954+
955955__all__ = ("Message" ,)
0 commit comments