-
Notifications
You must be signed in to change notification settings - Fork 945
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
Make sure we serialize dictionary into valid JSON #3408
Make sure we serialize dictionary into valid JSON #3408
Conversation
Does the JSON serialization already do this? It seems that JSON-encoding a dict with non-string keys already converts the key to a string:
|
From the python json documentation: https://docs.python.org/3/library/json.html#json.dumps
Is this not happening automatically for some dictionary? |
Yeah, it seems like non-primitive types in keys raise a TypeError:
I'm a bit concerned that we would be going beyond python json defaults here by automatically coercing non-primitive keys. |
Is the ipydatagrid issue the fact that keys are tuples? I'm sort of in favor of ipydatagrid having a custom serializer here, and keeping our base serialization in line with python defaults. Wouldn't ipydatagrid need a custom serializer on the js side anyway to interpret keys that are stringified tuples? |
ipydatagrid does have a custom serializer, but seeing as this was handled implicitly previously and now it's not, it will likely affect other custom widgets which do not use custom serializers. |
Oh, that is a different story. When did this work before? Is this an ipywidgets 7->8 change? |
It looks like this line is the same in the 7.x branch: ipywidgets/ipywidgets/widgets/widget.py Line 34 in 288ea22
|
Some background missing from my PR description: This was actually handled in ipykernel, which used to "clean" the JSON prior to pass the socket message. This logic has been removed from ipykernel for jupyter_client 7 in order to improve performances. So now jupyter_client expects valid JSON, but it falls back to the old JSON cleaning logic if the message is not valid, showing a warning if the message is not valid. Though it was not handling invalid dictionary keys, I pushed a fix for that in jupyter_client: jupyter/jupyter_client#752
I think this makes sense. I am fine not merging this PR and leave it to the custom widgets developers to make sure the JSON is valid. |
Thanks, that background context is very helpful. So are these two statements correct?
If those statements are correct, then I'd be in favor of not merging this PR and relying on the warning for ipywidgets users. Alternatively, I'd also be +1 on merging a fix like this for ipywidgets 7 (so it continues working even after the fallback is removed) and noting in the ipywidgets 8 documentation that a widget author should take care of this with a custom serializer for ipywidgets 8. |
As of today, it will unfortunately raise a
That's correct |
@martinRenou: which option do you think is best?
(or is there another option?) |
Maybe 2? We can probably consider that I'll let you close this PR if you agree. |
Sounds good, thanks. |
And thanks for fixing the error down to a warning in jupyter_client! |
Similar PR to jupyter-widgets/ipydatagrid#303
This makes sure the returned JSON is valid (dictionary keys are strings)