-
Notifications
You must be signed in to change notification settings - Fork 1.1k
JSON serialization panics when given numpy values #4030
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
diff --git a/cirq/protocols/json_serialization.py b/cirq/protocols/json_serialization.py
index 77bc8d8e..b1dcdd67 100644
--- a/cirq/protocols/json_serialization.py
+++ b/cirq/protocols/json_serialization.py
@@ -444,6 +444,8 @@ def has_serializable_by_keys(obj: Any) -> bool:
if isinstance(obj, Dict):
return any(has_serializable_by_keys(elem) for pair in obj.items() for elem in pair)
if hasattr(obj, '__iter__') and not isinstance(obj, str):
+ if isinstance(obj, np.ndarray) and obj.ndim==0:
+ return False
return any(has_serializable_by_keys(elem) for elem in obj)
return False is my hack. I don't know what the "true" fix would be. |
I have no better idea than your fix either. If feel like this is a highly special case where numpy is kind of breaking the iterable contract by having |
We could EAFP and try/catch Type error around the any() |
I have mixed feelings about using EAFP (it could lead to hard-to-find performance hits, depending on implementation), but overall agree with the proposed solution. The intent of this code is to locate key-serializable objects (like |
…nd not isinstance(obj, str):` to deal with a numpy special case (#4382) This `TypeError` is thrown from the `numpy` source: https://github.com/numpy/numpy/blob/ffcf508951f646c2ae02c2a0583b884f7a9163e8/numpy/core/src/multiarray/arrayobject.c#L1700-L1702 Fixes: #4030
…nd not isinstance(obj, str):` to deal with a numpy special case (quantumlib#4382) This `TypeError` is thrown from the `numpy` source: https://github.com/numpy/numpy/blob/ffcf508951f646c2ae02c2a0583b884f7a9163e8/numpy/core/src/multiarray/arrayobject.c#L1700-L1702 Fixes: quantumlib#4030
…nd not isinstance(obj, str):` to deal with a numpy special case (quantumlib#4382) This `TypeError` is thrown from the `numpy` source: https://github.com/numpy/numpy/blob/ffcf508951f646c2ae02c2a0583b884f7a9163e8/numpy/core/src/multiarray/arrayobject.c#L1700-L1702 Fixes: quantumlib#4030
This used to work. The above is a minimal example taken from real code that used to work with previous cirq versions.
The text was updated successfully, but these errors were encountered: