match case in micropython – SyntaxError: invalid syntax

Question:

I am using python 3.10.5 on my raspberry pi pico and I am trying to use match & case instead of if statements When I try to run the program it returns an error:

Traceback (most recent call last):
  File "<stdin>", line 22
SyntaxError: invalid syntax

Here is my function:

async def BlueTooth(delay):
    while True:
        if uart.any():
            command = uart.readline()
            #print(command)   # uncomment this line to see the received data
            match command:
                case b'1':
                    led.value(1)
                case b'0':
                    led.value(0)
            write_i2c("cmd: {}n{}".format(command, Commands.get_command_action(str(command))))

        await asyncio.sleep(delay)

I have checked, and everything should be normal, what can cause the problem?
BTW, I am using Thonny as my editor.

Asked By: David

||

Answers:

According to micropython’s README.md:

MicroPython implements the entire Python 3.4 syntax (including exceptions, with, yield from, etc., and additionally async/await keywords from Python 3.5 and some select features from later versions).

match/case are new as of Python 3.10, six releases after the last version of the Python language spec MicroPython claims full support for. And it’s a ridiculously complex addition to the language (relying on a complete replacement of the simpler LL(1) parser with a more flexible/powerful/complex PEG parser among other things). They don’t support it yet, but it’s on their todo list. When it’s supported, the "MicroPython differences from CPython ยป Python 3.10" docs should be updated to indicate that support is completed.

Answered By: ShadowRanger