Skip to content
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

dataclass_json_dict #4391

Merged
merged 6 commits into from
Aug 23, 2021
Merged

Conversation

mpharrigan
Copy link
Collaborator

@mpharrigan mpharrigan commented Aug 6, 2021

Dataclasses keep track of their relevant fields, so we can automatically generate these.
Dataclasses are implemented with somewhat complex metaprogramming, and tooling (PyCharm, mypy)
have special cases for dealing with classes decorated with @dataclass. There is very little
support (and no plans for support) for decorators that wrap @dataclass (like
@cirq.json_serializable_dataclass) or combining additional decorators with @dataclass.
Although not as elegant, you may want to consider explicitly defining _json_dict_ on your
dataclasses which simply return dataclass_json_dict(self).

@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Aug 6, 2021
@CirqBot CirqBot added size: XL lines changed >1000 size: S 10< lines changed <50 and removed size: XL lines changed >1000 labels Aug 12, 2021
@mpharrigan mpharrigan marked this pull request as ready for review August 12, 2021 23:02
@mpharrigan mpharrigan requested review from cduck, mrwojtek, vtomole and a team as code owners August 12, 2021 23:02
@mpharrigan mpharrigan requested a review from balopat August 12, 2021 23:02
Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this a lot. Can we deprecate json_serializable_dataclass?

@mpharrigan
Copy link
Collaborator Author

I wouldn't be opposed

Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, then LGTM.

@@ -500,6 +500,7 @@
is_parameterized,
JsonResolver,
json_serializable_dataclass,
dataclass_json_dict,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sort imports

@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Estimation of fidelity associated with experimental circuit executions."""
import dataclasses
from abc import abstractmethod, ABC
from dataclasses import dataclass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove this import and use @dataclasses.dataclass throughout.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -88,6 +88,7 @@
to_json,
read_json,
obj_to_dict_helper,
dataclass_json_dict,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say sort imports, but it looks like we haven't been doing that consistently. Really needs to be automated...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Craig and I are in solidarity that we won't manually sort imports

if TYPE_CHECKING:
from dataclasses import dataclass as json_serializable_dataclass
else:
from cirq.protocols import json_serializable_dataclass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would vote for removing this from the docs and deprecating json_serializable_dataclass, but I guess that could be a future PR.

Copy link
Collaborator Author

@mpharrigan mpharrigan Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the mypy hack. Propose punting the deprecation to a follow-on PR. opened #4460


Dataclasses keep track of their relevant fields, so we can automatically generate these.

Dataclasses are implemented with somewhat complex metaprogramming, and tooling (PyCharm, mypy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph is largely duplicated in the module docstring above. I'd suggest removing it here and relying on the module docstring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean module docstring? do you mean the docstring for json_serializable_dataclass? I think it's relevant to both functions. Especially if we remove json_serializable_dataclass

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I thought other occurence of this text was in the json_serialization.py module docstring. Agree it's fine to have on json_serializable_dataclass.

@@ -782,6 +782,24 @@ def custom_resolver(name):
assert_json_roundtrip_works(my_dc, resolvers=[custom_resolver] + cirq.DEFAULT_RESOLVERS)


def test_json_serializable_dataclass_no_decorator():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest naming this so it stands on its own, e.g. test_dataclass_json_dict (then no change needed if json_serializable_dataclass goes away).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

cirq-core/cirq/protocols/json_serialization_test.py Outdated Show resolved Hide resolved
@google-cla
Copy link

google-cla bot commented Aug 16, 2021

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no and removed cla: yes Makes googlebot stop complaining. labels Aug 16, 2021
@mpharrigan mpharrigan force-pushed the 2021-08-json-dataclass branch from befbb4e to 46c3bec Compare August 23, 2021 22:27
@google-cla
Copy link

google-cla bot commented Aug 23, 2021

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@mpharrigan
Copy link
Collaborator Author

@googlebot i fixed it

@google-cla
Copy link

google-cla bot commented Aug 23, 2021

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@mpharrigan mpharrigan force-pushed the 2021-08-json-dataclass branch from 46c3bec to c856b49 Compare August 23, 2021 22:28
@mpharrigan
Copy link
Collaborator Author

note to self: never ever accept suggestion in the github ui or clabot will lose it

@google-cla google-cla bot added cla: yes Makes googlebot stop complaining. and removed cla: no labels Aug 23, 2021
@mpharrigan
Copy link
Collaborator Author

@maffoo PTAL

Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


Dataclasses keep track of their relevant fields, so we can automatically generate these.

Dataclasses are implemented with somewhat complex metaprogramming, and tooling (PyCharm, mypy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I thought other occurence of this text was in the json_serialization.py module docstring. Agree it's fine to have on json_serializable_dataclass.

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Aug 23, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Aug 23, 2021
@CirqBot CirqBot merged commit 886fc51 into quantumlib:master Aug 23, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Aug 23, 2021
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
   Dataclasses keep track of their relevant fields, so we can automatically generate these.
    Dataclasses are implemented with somewhat complex metaprogramming, and tooling (PyCharm, mypy)
    have special cases for dealing with classes decorated with `@dataclass`. There is very little
    support (and no plans for support) for decorators that wrap `@dataclass` (like
    `@cirq.json_serializable_dataclass`) or combining additional decorators with `@dataclass`.
    Although not as elegant, you may want to consider explicitly defining `_json_dict_` on your
    dataclasses which simply `return dataclass_json_dict(self)`.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
   Dataclasses keep track of their relevant fields, so we can automatically generate these.
    Dataclasses are implemented with somewhat complex metaprogramming, and tooling (PyCharm, mypy)
    have special cases for dealing with classes decorated with `@dataclass`. There is very little
    support (and no plans for support) for decorators that wrap `@dataclass` (like
    `@cirq.json_serializable_dataclass`) or combining additional decorators with `@dataclass`.
    Although not as elegant, you may want to consider explicitly defining `_json_dict_` on your
    dataclasses which simply `return dataclass_json_dict(self)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining. size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants