Skip to content

Commit

Permalink
See protocolbuffers/protobuf#16596 breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Apr 25, 2024
1 parent 4089092 commit fb5f248
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions proto/marshal/compat.py
Expand Up @@ -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.
Expand All @@ -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",
)
6 changes: 5 additions & 1 deletion proto/marshal/marshal.py
Expand Up @@ -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)

Expand Down

0 comments on commit fb5f248

Please sign in to comment.