Skip to content

await difference with CPython #6140

@jcw

Description

@jcw

The current behaviour of await in a Python script differs between µPy and CPy.

Here's MicroPython 1.12:

>>> def a(x):
...     print(1)
...     await b(2)
...     print(3)
...
>>> a
<generator>
>>>

And here is CPython 3.8:

>>> def a(x):
...     print(1)
...     await b(2)
...     print(3)
...
  File "<stdin>", line 3
SyntaxError: 'await' outside async function
>>>

There are three ways to resolve this (at some point):

  1. document the difference
  2. make µPy reject with the same error as CPy
  3. do something new

That third option, would be to change the µPy behavior so that def a(): is not turned into a coroutine, but kept as regular function. This is 100% compatible with CPy, since to make the above work identically on both, you have to add the async keyword anyway.

Allowing await to be used in regular functions offers the opportunity to extend µPy in such a way that it solves the red/blue problem. I have some ideas on how to get there, and may actually have a go at implementing it, but it all hinges on which of the three decision µPy takes (at some point).

Either way, the above difference is real and will eventually need to be addressed, hence this issue.

Minor edit: await still needs an awaitable as arg. Nothing changed in that respect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    py-coreRelates to py/ directory in source

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions