Skip to content

Commit e2f9c9d

Browse files
authored
fix: fix conda compatibility issue (#475)
* fix: fix conda compatibility issue * add comment * typo
1 parent 4d8ee65 commit e2f9c9d

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

proto/marshal/compat.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@
2020

2121
from google.protobuf.internal import containers
2222

23-
# Import protobuf 4.xx first and fallback to earlier version
24-
# if not present.
23+
# Import all message types to ensure that pyext types are recognized
24+
# when upb types exist. Conda's protobuf defaults to pyext despite upb existing.
25+
# See https://github.com/googleapis/proto-plus-python/issues/470
2526
try:
26-
from google._upb import _message
27+
from google._upb import _message as _message_upb
2728
except ImportError:
28-
_message = None
29+
_message_upb = None
30+
31+
try:
32+
from google.protobuf.pyext import _message as _message_pyext
33+
except ImportError:
34+
_message_pyext = None
2935

30-
if not _message:
31-
try:
32-
from google.protobuf.pyext import _message
33-
except ImportError:
34-
_message = None
3536

3637
repeated_composite_types = (containers.RepeatedCompositeFieldContainer,)
3738
repeated_scalar_types = (containers.RepeatedScalarFieldContainer,)
@@ -44,16 +45,16 @@
4445
# See https://github.com/protocolbuffers/protobuf/issues/16596
4546
map_composite_type_names = ("MessageMapContainer",)
4647

47-
if _message:
48-
repeated_composite_types += (_message.RepeatedCompositeContainer,)
49-
repeated_scalar_types += (_message.RepeatedScalarContainer,)
50-
51-
try:
52-
map_composite_types += (_message.MessageMapContainer,)
53-
except AttributeError:
54-
# The `MessageMapContainer` attribute is not available in Protobuf 5.x+
55-
pass
48+
for message in [_message_upb, _message_pyext]:
49+
if message:
50+
repeated_composite_types += (message.RepeatedCompositeContainer,)
51+
repeated_scalar_types += (message.RepeatedScalarContainer,)
5652

53+
try:
54+
map_composite_types += (message.MessageMapContainer,)
55+
except AttributeError:
56+
# The `MessageMapContainer` attribute is not available in Protobuf 5.x+
57+
pass
5758

5859
__all__ = (
5960
"repeated_composite_types",

0 commit comments

Comments
 (0)