From 7121def569a2b99a65a6e7290dcc80ab175efbe3 Mon Sep 17 00:00:00 2001 From: Alexey Volkov Date: Mon, 8 Jul 2024 21:21:01 -0700 Subject: [PATCH] Fixed the compatibility with conda, protobuf 4 and pyext protobuf Fixes: https://github.com/googleapis/proto-plus-python/issues/470 Fixes: https://github.com/googleapis/python-aiplatform/issues/3129 --- proto/marshal/compat.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/proto/marshal/compat.py b/proto/marshal/compat.py index dc77f3ac..c0e916f7 100644 --- a/proto/marshal/compat.py +++ b/proto/marshal/compat.py @@ -30,12 +30,6 @@ except ImportError: _message = None -if not _message: - try: - from google.protobuf.pyext import _message - except ImportError: - _message = None - repeated_composite_types = (containers.RepeatedCompositeFieldContainer,) repeated_scalar_types = (containers.RepeatedScalarFieldContainer,) map_composite_types = (containers.MessageMap,) @@ -59,6 +53,25 @@ if PROTOBUF_VERSION[0:2] in ["3.", "4."]: map_composite_types += (_message.MessageMapContainer,) + +try: + from google.protobuf.pyext import _message as _message_pyext +except ImportError: + _message_pyext = None + +if _message_pyext: + repeated_composite_types += (_message_pyext.RepeatedCompositeContainer,) + repeated_scalar_types += (_message_pyext.RepeatedScalarContainer,) + + # In `proto/marshal.py`, for compatibility with protobuf 5.x, + # we'll use `map_composite_type_names` to check whether + # the name of the class of a protobuf type is + # `MessageMapContainer`, and, if `True`, return a MapComposite. + # See https://github.com/protocolbuffers/protobuf/issues/16596 + if PROTOBUF_VERSION[0:2] in ["3.", "4."]: + map_composite_types += (_message_pyext.MessageMapContainer,) + + __all__ = ( "repeated_composite_types", "repeated_scalar_types",