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

JS Number converted to <Javascript undefined> instead of <class 'float'> for async functions #2321

Closed
Daniel528 opened this issue Nov 13, 2023 · 8 comments

Comments

@Daniel528
Copy link

Hello,

stackblitz where bug is recreated: https://stackblitz.com/edit/js-mndfxt?file=index.js

I am attempting to return a float value from an async javascript function defined through the window and executed with the aio module. Inside the coroutine the python calls the function assigned to the window. The function returns a float which when read in python is of type <Javascript undefined> rather than of type <class 'float'>.

From testing currently it seems:

  • In the same circumstances, numbers returned async which are able to be converted to <class 'int'> are converted to <class 'int'>. It is not included in the stackblitz but I've also checked and strings and bools are converted to their respective types.
  • Functions which return a float synchronously return a value of type <class 'float'>
  • Async functions defined in the python script which return floats return a value of type <class 'float'> not as <Javascript undefined>
  • Where an async javascript function returns an object, where a child property is a float, the float is converted to type <class 'float'>.

Based on the above I am assuming the conversion of number to <Javascript undefined> for floats is a bug and not expected behaviour.

@denis-migdal
Copy link
Contributor

Can you try again using this PR : #2300 ?

@Daniel528
Copy link
Author

Daniel528 commented Nov 13, 2023

I am not sure what you mean. Based on that PR would you like me to try and add a custom JS -> Py conversion method to overwrite the current circumstance which is leading to <Javascript undefined>.

If that is the case, does that meant that by default the converting to <Javascript undefined> rather than <class 'float'> is the expected behaviour. In my head by default it should be converting to <class 'float'>. Relying on each user adding their own custom convertor methods is not solving the underlying issue if my assumption that it is an issue is correct.

@denis-migdal
Copy link
Contributor

I am not sure what you mean. Based on that PR would you like me to try and add a custom JS -> Py conversion method to overwrite the current circumstance which is leading to .

No, I just want to see if the code in this PR works in your case in order to investigate the cause of your issue.

@Daniel528
Copy link
Author

I am not sure what you mean. Based on that PR would you like me to try and add a custom JS -> Py conversion method to overwrite the current circumstance which is leading to .

No, I just want to see if the code in this PR works in your case in order to investigate the cause of your issue.

Cool. Let me know if this is what you're after: https://stackblitz.com/edit/js-8tw6yo?file=index.html

Same stackblitz as above but loading brython scripts from the #2300

@denis-migdal
Copy link
Contributor

Hum, it seems to work with my version ?

Gotta do the changes (maybe at the end of the week?) requested by @PierreQuentel so that the PR is merged.

@Daniel528
Copy link
Author

Daniel528 commented Nov 13, 2023

Hum, it seems to work with my version ?

Gotta do the changes (maybe at the end of the week?) requested by @PierreQuentel so that the PR is merged.

This is the console output I see for the stackblitz using the forked brython version:

[Log] Begin Python Execution (preview-c97e327a318ca.js, line 2)

[Log] awaiting... (preview-c97e327a318ca.js, line 2)

[Log] async int (preview-c97e327a318ca.js, line 2)

[Log] 10 (preview-c97e327a318ca.js, line 2)

[Log] <class 'int'> (preview-c97e327a318ca.js, line 2)

[Log] ------- (preview-c97e327a318ca.js, line 2)

[Log] async float (preview-c97e327a318ca.js, line 2)

[Log] 10.12 (preview-c97e327a318ca.js, line 2)

[Log] <class 'JSObject'> (preview-c97e327a318ca.js, line 2)

[Log] ------- (preview-c97e327a318ca.js, line 2)

[Log] sync float (preview-c97e327a318ca.js, line 2)

[Log] 10.12 (preview-c97e327a318ca.js, line 2)

[Log] <class 'float'> (preview-c97e327a318ca.js, line 2)

[Log] ------- (preview-c97e327a318ca.js, line 2)

[Error] TypeError: undefined is not an object (evaluating 'pyobj[JSOBJ]')
	(anonymous function) (preview-c97e327a318ca.js:2:571672)
	(anonymous function) (brython.js:15190)
	method (brython.js:5994)
	(anonymous function) (brython.js:8570)
	(anonymous function) (brython.js:8574)
	jsobj (brython.js:13948)
[Log] async future float (preview-c97e327a318ca.js, line 2)

[Log] 10.21 (preview-c97e327a318ca.js, line 2)

[Log] <class 'float'> (preview-c97e327a318ca.js, line 2)

[Log] ------ (preview-c97e327a318ca.js, line 2)

[Error] TypeError: undefined is not an object (evaluating 'pyobj[JSOBJ]')
	(anonymous function) (brython.js:8575)
	jsobj (brython.js:13948)
[Log] async jsObject (preview-c97e327a318ca.js, line 2)

[Log] <Javascript object: [object Object]> (preview-c97e327a318ca.js, line 2)

[Log] <class 'JSObject'> (preview-c97e327a318ca.js, line 2)

[Log] 10 (preview-c97e327a318ca.js, line 2)

[Log] <class 'int'> (preview-c97e327a318ca.js, line 2)

[Log] 10.12 (preview-c97e327a318ca.js, line 2)

[Log] <class 'float'> (preview-c97e327a318ca.js, line 2)

The logged type using of the converted float is <class 'JSObject'>. My assumption would be the working behaviour would instead have it be <class 'float'>

@denis-migdal
Copy link
Contributor

Maybe an issue related to $B.promise() ( cf also issue #2320 ) ?

Wednesday I'll try to take a deeper look on it (except if Pierre solves it first).

@PierreQuentel
Copy link
Contributor

Thanks to both of you :-)

The issue should be solved, but if not please comment again, I will reopen it.

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

No branches or pull requests

3 participants