Skip to content

Commit bae02e4

Browse files
authored
Allow using protobuf version 5 and later (#6683)
Review: @pavoljuhas
1 parent c8f799e commit bae02e4

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

cirq-google/cirq_google/serialization/arg_func_langs_test.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import inspect
16+
from typing import Dict
17+
1518
import numpy as np
1619
import pytest
1720
import sympy
18-
1921
from google.protobuf import json_format
2022

2123
import cirq_google
@@ -35,6 +37,19 @@
3537
from cirq.qis import CliffordTableau
3638

3739

40+
def _json_format_kwargs() -> Dict[str, bool]:
41+
"""Determine kwargs to pass to json_format.MessageToDict.
42+
43+
Protobuf v5 has a different signature for MessageToDict. If we ever move to requiring
44+
protobuf >= 5 this can be removed.
45+
"""
46+
sig = inspect.signature(json_format.MessageToDict)
47+
new_arg = "always_print_fields_with_no_presence"
48+
old_arg = "including_default_value_fields"
49+
arg = new_arg if new_arg in sig.parameters else old_arg
50+
return {arg: True}
51+
52+
3853
@pytest.mark.parametrize(
3954
'min_lang,value,proto',
4055
[
@@ -85,7 +100,7 @@ def test_correspondence(min_lang: str, value: ARG_LIKE, proto: v2.program_pb2.Ar
85100
parsed = arg_from_proto(msg, arg_function_language=lang)
86101
packed = json_format.MessageToDict(
87102
arg_to_proto(value, arg_function_language=lang),
88-
including_default_value_fields=True,
103+
**_json_format_kwargs(),
89104
preserving_proto_field_name=True,
90105
use_integers_for_enums=True,
91106
)
@@ -109,7 +124,7 @@ def test_serialize_sympy_constants():
109124
proto = arg_to_proto(sympy.pi, arg_function_language='')
110125
packed = json_format.MessageToDict(
111126
proto,
112-
including_default_value_fields=True,
127+
**_json_format_kwargs(),
113128
preserving_proto_field_name=True,
114129
use_integers_for_enums=True,
115130
)
@@ -153,7 +168,7 @@ def test_serialize_conversion(value: ARG_LIKE, proto: v2.program_pb2.Arg):
153168
json_format.ParseDict(proto, msg)
154169
packed = json_format.MessageToDict(
155170
arg_to_proto(value, arg_function_language=''),
156-
including_default_value_fields=True,
171+
**_json_format_kwargs(),
157172
preserving_proto_field_name=True,
158173
use_integers_for_enums=True,
159174
)

cirq-google/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
google-api-core[grpc] >= 1.14.0
22
proto-plus >= 1.20.0
3-
protobuf >= 3.15.0, < 5.0.0
3+
protobuf >= 3.15.0

dev_tools/requirements/deps/mypy.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ mypy==1.2.0
44
# packages with stub types for various libraries
55
types-backports==0.1.3
66
types-cachetools
7-
types-protobuf>=3.20.0,<5.0.0
7+
types-protobuf>=3.20.0
88
types-requests==2.28.1
99
types-setuptools==62.6.1

0 commit comments

Comments
 (0)