12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import inspect
16
+ from typing import Dict
17
+
15
18
import numpy as np
16
19
import pytest
17
20
import sympy
18
-
19
21
from google .protobuf import json_format
20
22
21
23
import cirq_google
35
37
from cirq .qis import CliffordTableau
36
38
37
39
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
+
38
53
@pytest .mark .parametrize (
39
54
'min_lang,value,proto' ,
40
55
[
@@ -85,7 +100,7 @@ def test_correspondence(min_lang: str, value: ARG_LIKE, proto: v2.program_pb2.Ar
85
100
parsed = arg_from_proto (msg , arg_function_language = lang )
86
101
packed = json_format .MessageToDict (
87
102
arg_to_proto (value , arg_function_language = lang ),
88
- including_default_value_fields = True ,
103
+ ** _json_format_kwargs () ,
89
104
preserving_proto_field_name = True ,
90
105
use_integers_for_enums = True ,
91
106
)
@@ -109,7 +124,7 @@ def test_serialize_sympy_constants():
109
124
proto = arg_to_proto (sympy .pi , arg_function_language = '' )
110
125
packed = json_format .MessageToDict (
111
126
proto ,
112
- including_default_value_fields = True ,
127
+ ** _json_format_kwargs () ,
113
128
preserving_proto_field_name = True ,
114
129
use_integers_for_enums = True ,
115
130
)
@@ -153,7 +168,7 @@ def test_serialize_conversion(value: ARG_LIKE, proto: v2.program_pb2.Arg):
153
168
json_format .ParseDict (proto , msg )
154
169
packed = json_format .MessageToDict (
155
170
arg_to_proto (value , arg_function_language = '' ),
156
- including_default_value_fields = True ,
171
+ ** _json_format_kwargs () ,
157
172
preserving_proto_field_name = True ,
158
173
use_integers_for_enums = True ,
159
174
)
0 commit comments