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
Add JsProxy.to_py method #1244
Add JsProxy.to_py method #1244
Conversation
Here's one limitation of the current implementation: z = new Map();
z.set([1,1], 2);
pyodide.runPython(`
from js import z
z.to_py() # ==> TypeError: unhashable type: 'list'
`); We could try to convert lists in keys to tuples, but then there's also: z = new Map();
z.set({"a" : 2}, 2);
pyodide.runPython(`
from js import z
z.to_py() # ==> TypeError: unhashable type: 'dict'
`); and: z = new Map();
class Test {};
z.set(new Test(), 2);
pyodide.runPython(`
from js import z
z.to_py() # ==> TypeError: unhashable type: 'JsProxy'
`); Of course the same problems happen when converting |
I think that's an expected error from the Python perspective on unhashable keys, so better to raise it than try to find some non obvious workaround (such as converting keys to str). |
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.
Hello @hoodmane,
thanks for your work! I'm mostly well with this pull request, but what do you think about adding the failing tests as well to the test suite? See my last comment above.
@phorward Thanks for the review! |
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.
OK, I'm fine with this, thanks for updating the tests!
Thanks! I have not reviewed the JS code in detail but the tests look good. Merging with a +1 above. |
Currently converts
Map
todict
,Set
toset
and array-like objects tolist
. If the object is a baseObject
(not a subclass ofObject
, I check thatObject.constructor.name === "object"
and that it doesn't have a customSymbol.toStringTag
) then I convert it to adict
as well. Otherwise I leave it alone.Takes a depth parameter which works in the same way as
PyProxy.toJs
.