|
20 | 20 |
|
21 | 21 | from google.protobuf.internal import containers |
22 | 22 |
|
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 |
25 | 26 | try: |
26 | | - from google._upb import _message |
| 27 | + from google._upb import _message as _message_upb |
27 | 28 | 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 |
29 | 35 |
|
30 | | -if not _message: |
31 | | - try: |
32 | | - from google.protobuf.pyext import _message |
33 | | - except ImportError: |
34 | | - _message = None |
35 | 36 |
|
36 | 37 | repeated_composite_types = (containers.RepeatedCompositeFieldContainer,) |
37 | 38 | repeated_scalar_types = (containers.RepeatedScalarFieldContainer,) |
|
44 | 45 | # See https://github.com/protocolbuffers/protobuf/issues/16596 |
45 | 46 | map_composite_type_names = ("MessageMapContainer",) |
46 | 47 |
|
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,) |
56 | 52 |
|
| 53 | + try: |
| 54 | + map_composite_types += (message.MessageMapContainer,) |
| 55 | + except AttributeError: |
| 56 | + # The `MessageMapContainer` attribute is not available in Protobuf 5.x+ |
| 57 | + pass |
57 | 58 |
|
58 | 59 | __all__ = ( |
59 | 60 | "repeated_composite_types", |
|
0 commit comments