From fb5f2483fef3bc946630d8e111f3ee64be8cda5d Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 25 Apr 2024 14:42:39 +0000 Subject: [PATCH] See https://github.com/protocolbuffers/protobuf/issues/16596;avoid breaking change --- proto/marshal/compat.py | 9 +++++++-- proto/marshal/marshal.py | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/proto/marshal/compat.py b/proto/marshal/compat.py index e10999bb..0c11d705 100644 --- a/proto/marshal/compat.py +++ b/proto/marshal/compat.py @@ -19,6 +19,9 @@ # not be included. from google.protobuf.internal import containers +import google.protobuf + +PROTOBUF_VERSION = google.protobuf.__version__ # Import protobuf 4.xx first and fallback to earlier version # if not present. @@ -36,15 +39,17 @@ repeated_composite_types = (containers.RepeatedCompositeFieldContainer,) repeated_scalar_types = (containers.RepeatedScalarFieldContainer,) map_composite_types = (containers.MessageMap,) +map_composite_types_str = ('MessageMapContainer') if _message: repeated_composite_types += (_message.RepeatedCompositeContainer,) repeated_scalar_types += (_message.RepeatedScalarContainer,) - map_composite_types += (_message.MessageMapContainer,) - + if PROTOBUF_VERSION[0:2] in ["3.", "4."]: + map_composite_types += (_message.MessageMapContainer,) __all__ = ( "repeated_composite_types", "repeated_scalar_types", "map_composite_types", + "map_composite_types_str", ) diff --git a/proto/marshal/marshal.py b/proto/marshal/marshal.py index e9fbbb9f..783c8417 100644 --- a/proto/marshal/marshal.py +++ b/proto/marshal/marshal.py @@ -188,7 +188,11 @@ def to_python(self, proto_type, value, *, absent: bool = None): return Repeated(value, marshal=self) # Same thing for maps of messages. - if value_type in compat.map_composite_types: + # See https://github.com/protocolbuffers/protobuf/issues/16596 + # We need to look up the name of the type in compat.map_composite_types_str + # as class `MessageMapContainer` is no longer exposed + # This is done to avoid taking a breaking change in proto-plus + if value_type in compat.map_composite_types or value_type.__name__ in compat.map_composite_types_str: return MapComposite(value, marshal=self) return self.get_rule(proto_type=proto_type).to_python(value, absent=absent)