-
Notifications
You must be signed in to change notification settings - Fork 1k
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
dataclass_json_dict #4391
Conversation
There was a problem hiding this 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
?
I wouldn't be opposed |
There was a problem hiding this 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, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
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. ℹ️ Googlers: Go here for more info. |
befbb4e
to
46c3bec
Compare
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. ℹ️ Googlers: Go here for more info. |
@googlebot i fixed it |
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. ℹ️ Googlers: Go here for more info. |
46c3bec
to
c856b49
Compare
note to self: never ever accept suggestion in the github ui or clabot will lose it |
@maffoo PTAL |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
.
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)`.
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)`.
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 littlesupport (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 yourdataclasses which simply
return dataclass_json_dict(self)
.