Skip to content

Commit 813f4f5

Browse files
95-martin-orionrht
authored andcommitted
Don't invoke json_cirq_type if cirq_type is defined...for now. (quantumlib#4711)
Fixes a backwards-incompatibility in quantumlib#4704. This alternate return path is necessary until the deprecation cycle for this change is complete.
1 parent b8bd6c8 commit 813f4f5

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

cirq-core/cirq/protocols/json_serialization.py

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def _json_dict_with_cirq_type(obj: Any):
286286
"an error starting in Cirq v0.15.",
287287
DeprecationWarning,
288288
)
289+
return base_dict
289290
return {'cirq_type': json_cirq_type(type(obj)), **base_dict}
290291

291292

cirq-core/cirq/protocols/json_serialization_test.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pathlib
2222
import sys
2323
import warnings
24-
from typing import Dict, List, Optional, Tuple, Type
24+
from typing import ClassVar, Dict, List, Optional, Tuple, Type
2525
from unittest import mock
2626

2727
import numpy as np
@@ -77,18 +77,24 @@ def _get_testspecs_for_modules() -> List[ModuleJsonTestSpec]:
7777

7878
def test_deprecated_cirq_type_in_json_dict():
7979
class HasOldJsonDict:
80+
# Required for testing serialization of non-cirq objects.
81+
__module__ = 'test.noncirq.namespace' # type: ignore
82+
8083
def __eq__(self, other):
8184
return isinstance(other, HasOldJsonDict)
8285

8386
def _json_dict_(self):
84-
return {'cirq_type': 'cirq.testing.HasOldJsonDict'}
87+
return {'cirq_type': 'test.noncirq.namespace.HasOldJsonDict'}
8588

8689
@classmethod
8790
def _from_json_dict_(cls, **kwargs):
8891
return cls()
8992

93+
with pytest.raises(ValueError, match='not a Cirq type'):
94+
_ = cirq.json_cirq_type(HasOldJsonDict)
95+
9096
def custom_resolver(name):
91-
if name == 'cirq.testing.HasOldJsonDict':
97+
if name == 'test.noncirq.namespace.HasOldJsonDict':
9298
return HasOldJsonDict
9399

94100
test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS
@@ -98,21 +104,29 @@ def custom_resolver(name):
98104

99105
def test_deprecated_obj_to_dict_helper_namespace():
100106
class HasOldJsonDict:
107+
# Required for testing serialization of non-cirq objects.
108+
__module__ = 'test.noncirq.namespace' # type: ignore
109+
101110
def __init__(self, x):
102111
self.x = x
103112

104113
def __eq__(self, other):
105114
return isinstance(other, HasOldJsonDict) and other.x == self.x
106115

107116
def _json_dict_(self):
108-
return json_serialization.obj_to_dict_helper(self, ['x'], namespace='cirq.testing')
117+
return json_serialization.obj_to_dict_helper(
118+
self, ['x'], namespace='test.noncirq.namespace'
119+
)
109120

110121
@classmethod
111122
def _from_json_dict_(cls, x, **kwargs):
112123
return cls(x)
113124

125+
with pytest.raises(ValueError, match='not a Cirq type'):
126+
_ = cirq.json_cirq_type(HasOldJsonDict)
127+
114128
def custom_resolver(name):
115-
if name == 'cirq.testing.HasOldJsonDict':
129+
if name == 'test.noncirq.namespace.HasOldJsonDict':
116130
return HasOldJsonDict
117131

118132
test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS
@@ -125,17 +139,22 @@ def custom_resolver(name):
125139
def test_deprecated_dataclass_json_dict_namespace():
126140
@dataclasses.dataclass
127141
class HasOldJsonDict:
142+
# Required for testing serialization of non-cirq objects.
143+
__module__: ClassVar = 'test.noncirq.namespace' # type: ignore
128144
x: int
129145

130146
def _json_dict_(self):
131-
return json_serialization.dataclass_json_dict(self, namespace='cirq.testing')
147+
return json_serialization.dataclass_json_dict(self, namespace='test.noncirq.namespace')
132148

133149
@classmethod
134150
def _from_json_dict_(cls, x, **kwargs):
135151
return cls(x)
136152

153+
with pytest.raises(ValueError, match='not a Cirq type'):
154+
_ = cirq.json_cirq_type(HasOldJsonDict)
155+
137156
def custom_resolver(name):
138-
if name == 'cirq.testing.HasOldJsonDict':
157+
if name == 'test.noncirq.namespace.HasOldJsonDict':
139158
return HasOldJsonDict
140159

141160
test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS

0 commit comments

Comments
 (0)