Skip to content

Don't invoke json_cirq_type if cirq_type is defined...for now. #4711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cirq-core/cirq/protocols/json_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def _json_dict_with_cirq_type(obj: Any):
"an error starting in Cirq v0.15.",
DeprecationWarning,
)
return base_dict
return {'cirq_type': json_cirq_type(type(obj)), **base_dict}


Expand Down
33 changes: 26 additions & 7 deletions cirq-core/cirq/protocols/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pathlib
import sys
import warnings
from typing import Dict, List, Optional, Tuple, Type
from typing import ClassVar, Dict, List, Optional, Tuple, Type
from unittest import mock

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

def test_deprecated_cirq_type_in_json_dict():
class HasOldJsonDict:
# Required for testing serialization of non-cirq objects.
__module__ = 'test.noncirq.namespace' # type: ignore

def __eq__(self, other):
return isinstance(other, HasOldJsonDict)

def _json_dict_(self):
return {'cirq_type': 'cirq.testing.HasOldJsonDict'}
return {'cirq_type': 'test.noncirq.namespace.HasOldJsonDict'}

@classmethod
def _from_json_dict_(cls, **kwargs):
return cls()

with pytest.raises(ValueError, match='not a Cirq type'):
_ = cirq.json_cirq_type(HasOldJsonDict)

def custom_resolver(name):
if name == 'cirq.testing.HasOldJsonDict':
if name == 'test.noncirq.namespace.HasOldJsonDict':
return HasOldJsonDict

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

def test_deprecated_obj_to_dict_helper_namespace():
class HasOldJsonDict:
# Required for testing serialization of non-cirq objects.
__module__ = 'test.noncirq.namespace' # type: ignore

def __init__(self, x):
self.x = x

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

def _json_dict_(self):
return json_serialization.obj_to_dict_helper(self, ['x'], namespace='cirq.testing')
return json_serialization.obj_to_dict_helper(
self, ['x'], namespace='test.noncirq.namespace'
)

@classmethod
def _from_json_dict_(cls, x, **kwargs):
return cls(x)

with pytest.raises(ValueError, match='not a Cirq type'):
_ = cirq.json_cirq_type(HasOldJsonDict)

def custom_resolver(name):
if name == 'cirq.testing.HasOldJsonDict':
if name == 'test.noncirq.namespace.HasOldJsonDict':
return HasOldJsonDict

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

def _json_dict_(self):
return json_serialization.dataclass_json_dict(self, namespace='cirq.testing')
return json_serialization.dataclass_json_dict(self, namespace='test.noncirq.namespace')

@classmethod
def _from_json_dict_(cls, x, **kwargs):
return cls(x)

with pytest.raises(ValueError, match='not a Cirq type'):
_ = cirq.json_cirq_type(HasOldJsonDict)

def custom_resolver(name):
if name == 'cirq.testing.HasOldJsonDict':
if name == 'test.noncirq.namespace.HasOldJsonDict':
return HasOldJsonDict

test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS
Expand Down