-
Notifications
You must be signed in to change notification settings - Fork 1.1k
JSON serialization: namespaces for cirq_type #4377
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
Comments
#4527 adds a hook into the current testing infrastructure to make it possible to make namespaced objects pass the tests, although it requires manually adding a mapping from unqualified class name to cirq_type, which doesn't solve the disambiguation problem. |
Tentatively putting pre-1.0 onto this issue. It sounds like this issue is mostly done. Tagging relevant people to update the issue and retriage. |
cc #4698 Namespacing has received some upgrades for users of Cirq. The testing infrastructure is what isn't set up for it. There's a workaround merged into the testing Moving to time/after-1.0 and noting that this should be part of a broader json testing rework. |
To add some data points, the discussion of namespaces came up in two places recently:
I would advocate for requiring all newly created classes in vendor directories to have a JSON namespace in order to minimize future collisions. If the full enforcement mechanism will take time to build, we can start by documenting the policy, and cirq-maintainers could act as gatekeepers for now. If we fix this only after seeing lots of collisions, it would be too late because we can't change existing JSON serializations AFAIU. |
This is actually really easy to enforce. Currently all classes whose module starts with 'cirq' default to a blank namespace: Cirq/cirq-core/cirq/protocols/json_serialization.py Lines 593 to 597 in 6e0e164
Changing line 595 to |
You could have an explicit list of grandfathered-in exceptions on line 595. Not the prettiest thing but effective |
@95-martin-orion didn't you implement a requirement for namespaces to be required? |
It looks like #4693 does this (and more specifically this line, as seen in my previous comment), but we never went as far as my suggestion above. Notably, Probably the reason for this is that cirq_google has several namespace-less types to work around, and this wasn't a priority in the push to v1.0. |
Right now in cirq and all modules in the main cirq repo, the
cirq_type
field used in json serialization is the unqualified object name. This has not caused any naming collisions yet.I had always imagined that different packages would have a namespaced cirq_type. For example:
cirq_google.PhysicalZTag
orcirq.google.PhysicalZTag
instead ofPhysicalZTag
.In fact,
cirq.obj_to_dict_helper
has an optionalnamespace
argument. I tried to use this for a newcirq_google
object, but the testing procedure uses the unqualified object name to assert that a data file should exist.While playing around with #4375 I think it would be more robust to decouple the "object generation" from any semantic meaning. Instead, we generate all the example objects and keep track of the types we've successfully round-tripped. Afterwards we can check if there are public objects that weren't round-tripped.
The text was updated successfully, but these errors were encountered: