Skip to content
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

Merged
merged 11 commits into from Feb 24, 2021
Merged

Add JsProxy.to_py method #1244

merged 11 commits into from Feb 24, 2021

Conversation

hoodmane
Copy link
Member

@hoodmane hoodmane commented Feb 12, 2021

Currently converts Map to dict, Set to set and array-like objects to list. If the object is a base Object (not a subclass of Object, I check that Object.constructor.name === "object" and that it doesn't have a custom Symbol.toStringTag) then I convert it to a dict as well. Otherwise I leave it alone.

Takes a depth parameter which works in the same way as PyProxy.toJs.

@hoodmane
Copy link
Member Author

hoodmane commented Feb 12, 2021

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 Set to set.

@rth
Copy link
Member

rth commented Feb 13, 2021

Here's one limitation of the current implementation:
TypeError: unhashable type

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).

Copy link
Contributor

@phorward phorward left a 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.

src/core/js2python.c Outdated Show resolved Hide resolved
src/core/js2python.c Outdated Show resolved Hide resolved
src/tests/test_typeconversions.py Show resolved Hide resolved
@hoodmane
Copy link
Member Author

@phorward Thanks for the review!

Copy link
Contributor

@phorward phorward left a 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!

@rth
Copy link
Member

rth commented Feb 24, 2021

Thanks! I have not reviewed the JS code in detail but the tests look good. Merging with a +1 above.

@rth rth merged commit 3e460af into pyodide:master Feb 24, 2021
@hoodmane hoodmane deleted the convert-to-py branch February 24, 2021 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants