Skip to content

Mark module as allowing await in global code #144072

@jolaf

Description

@jolaf

Feature or enhancement

Proposal:

With the advent of Pyodide/PyScript, we have a situation where a Python module gets loaded in async mode with event loop already running, and contains await in module global code, like this:

async def main() -> None:
    ...

if __name__ == '__main__':
    await main()

This works fine, however, static code checkers and IDEs get crazy about "you cannot use await in global code", treat it as hard error, highlight the module as erroneous in IDE project etc.

It would be nice to have a universal way to mark a module like "supposed to be used in async context", so that static checkers and IDEs could see that mark and adjust their behavior accordingly.

It can be a # async comment in the first line or from __future__ import asyncmodule or something else statically readable. There's no need for runtime to react on this mark in any way.

PyCF_ALLOW_TOP_LEVEL_AWAIT resolves the situation in runtime, but unfortunately doesn't help in this matter because it's not visible in static context.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

pyodide/pyodide#6065
pyscript/pyscript#2425

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions