21
21
import pathlib
22
22
import sys
23
23
import warnings
24
- from typing import Dict , List , Optional , Tuple , Type
24
+ from typing import ClassVar , Dict , List , Optional , Tuple , Type
25
25
from unittest import mock
26
26
27
27
import numpy as np
@@ -77,18 +77,24 @@ def _get_testspecs_for_modules() -> List[ModuleJsonTestSpec]:
77
77
78
78
def test_deprecated_cirq_type_in_json_dict ():
79
79
class HasOldJsonDict :
80
+ # Required for testing serialization of non-cirq objects.
81
+ __module__ = 'test.noncirq.namespace' # type: ignore
82
+
80
83
def __eq__ (self , other ):
81
84
return isinstance (other , HasOldJsonDict )
82
85
83
86
def _json_dict_ (self ):
84
- return {'cirq_type' : 'cirq.testing .HasOldJsonDict' }
87
+ return {'cirq_type' : 'test.noncirq.namespace .HasOldJsonDict' }
85
88
86
89
@classmethod
87
90
def _from_json_dict_ (cls , ** kwargs ):
88
91
return cls ()
89
92
93
+ with pytest .raises (ValueError , match = 'not a Cirq type' ):
94
+ _ = cirq .json_cirq_type (HasOldJsonDict )
95
+
90
96
def custom_resolver (name ):
91
- if name == 'cirq.testing .HasOldJsonDict' :
97
+ if name == 'test.noncirq.namespace .HasOldJsonDict' :
92
98
return HasOldJsonDict
93
99
94
100
test_resolvers = [custom_resolver ] + cirq .DEFAULT_RESOLVERS
@@ -98,21 +104,29 @@ def custom_resolver(name):
98
104
99
105
def test_deprecated_obj_to_dict_helper_namespace ():
100
106
class HasOldJsonDict :
107
+ # Required for testing serialization of non-cirq objects.
108
+ __module__ = 'test.noncirq.namespace' # type: ignore
109
+
101
110
def __init__ (self , x ):
102
111
self .x = x
103
112
104
113
def __eq__ (self , other ):
105
114
return isinstance (other , HasOldJsonDict ) and other .x == self .x
106
115
107
116
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
+ )
109
120
110
121
@classmethod
111
122
def _from_json_dict_ (cls , x , ** kwargs ):
112
123
return cls (x )
113
124
125
+ with pytest .raises (ValueError , match = 'not a Cirq type' ):
126
+ _ = cirq .json_cirq_type (HasOldJsonDict )
127
+
114
128
def custom_resolver (name ):
115
- if name == 'cirq.testing .HasOldJsonDict' :
129
+ if name == 'test.noncirq.namespace .HasOldJsonDict' :
116
130
return HasOldJsonDict
117
131
118
132
test_resolvers = [custom_resolver ] + cirq .DEFAULT_RESOLVERS
@@ -125,17 +139,22 @@ def custom_resolver(name):
125
139
def test_deprecated_dataclass_json_dict_namespace ():
126
140
@dataclasses .dataclass
127
141
class HasOldJsonDict :
142
+ # Required for testing serialization of non-cirq objects.
143
+ __module__ : ClassVar = 'test.noncirq.namespace' # type: ignore
128
144
x : int
129
145
130
146
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 ' )
132
148
133
149
@classmethod
134
150
def _from_json_dict_ (cls , x , ** kwargs ):
135
151
return cls (x )
136
152
153
+ with pytest .raises (ValueError , match = 'not a Cirq type' ):
154
+ _ = cirq .json_cirq_type (HasOldJsonDict )
155
+
137
156
def custom_resolver (name ):
138
- if name == 'cirq.testing .HasOldJsonDict' :
157
+ if name == 'test.noncirq.namespace .HasOldJsonDict' :
139
158
return HasOldJsonDict
140
159
141
160
test_resolvers = [custom_resolver ] + cirq .DEFAULT_RESOLVERS
0 commit comments