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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy