pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

Top-Level-Await Code Detection Misses Some Cases

Open JeffersGlass opened this issue 3 years ago • 0 comments

Checklist

  • [X] I added a descriptive title
  • [X] I searched for other issues and couldn't find a solution or duplication
  • [X] I already searched in Google and didn't find any good information or help

What happened?

When a <py-script> tag is evaluated, PyScript must determine whether to use runPython() or runPythonAsync() to run the enclosed code. The current method for this is to check if the string 'asyncio' is present in the source. This is fast, but incorrectly identifies causes some code to error that would run fine with top level wait enabled.

For example, this gives a SyntaxError: 'await' outside function :

from pyodide.http import pyfetch
response = await pyfetch('https://jsonplaceholder.typicode.com/todos/1')
text = await response.json()
print(text)

But this runs fine (note the comment on the first line):

# asyncio
from pyodide.http import pyfetch
response = await pyfetch('https://jsonplaceholder.typicode.com/todos/1')
text = await response.json()
print(text)

What browsers are you seeing the problem on? (if applicable)

Chrome

Console info

No response

Additional Context

I will submit a PR for an improved method of detecting the need for top-level-await presently.

JeffersGlass avatar Aug 23 '22 19:08 JeffersGlass