-
Notifications
You must be signed in to change notification settings - Fork 1.1k
dataclass_json_dict #4391
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
Changes from all commits
c98728b
d768570
e083c6d
9db9f91
e6c184d
c856b49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 commentThe 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 |
||
SerializableByKey, | ||
SupportsJSON, | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,13 @@ def json_serializable_dataclass( | |
the ``_json_dict_`` protocol method which automatically determines | ||
the appropriate fields from the dataclass. | ||
|
||
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 this. Consider explicitly | ||
defining `_json_dict_` on your dataclasses which simply | ||
`return dataclass_json_dict(self)`. | ||
|
||
Args: | ||
namespace: An optional prefix to the value associated with the | ||
key "cirq_type". The namespace name will be joined with the | ||
|
@@ -209,6 +216,21 @@ def wrap(cls): | |
# pylint: enable=redefined-builtin | ||
|
||
|
||
def dataclass_json_dict(obj: Any, namespace: str = None) -> Dict[str, Any]: | ||
"""Return a dictionary suitable for _json_dict_ from a 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry, I thought other occurence of this text was in the |
||
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)`. | ||
""" | ||
return obj_to_dict_helper(obj, [f.name for f in dataclasses.fields(obj)], namespace=namespace) | ||
|
||
|
||
class CirqEncoder(json.JSONEncoder): | ||
"""Extend json.JSONEncoder to support Cirq objects. | ||
|
||
|
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