Syntax Error when using multiline f-strings on RP2040 with MicroPython v1.23.0
Port, board and/or hardware
RP2040 Raspberry Pi Pico
MicroPython version
MicroPython Version: v1.23.0 (2024-06-02)
RP2040 Platform: Raspberry Pi Pico
Python Version: MicroPython (RP2040)
Firmware: v1.23.0
Hardware: Raspberry Pi Pico (RP2040)
Reproduction
msg = (
f'One '
f'Two '
f'Three'
)
print(msg)
Expected behaviour
The above code should print the concatenated string One Two Three without any syntax errors, similar to how it works in standard Python environments.
Observed behaviour
SyntaxError: invalid syntax
Additional Information
This issue persists even when re-typing the code, leading me to suspect it might be an issue with the way multiline f-strings are handled on this particular platform.
The issue can be resolved by concatenating the f-strings manually using +:
msg = (
f'One ' +
f'Two ' +
f'Three'
)
print(msg)
Workaround:
Using string concatenation (as shown above) works, but this is not as elegant and adds unnecessary complexity compared to expected behaviour.
What else I have tried:
Testing with other Python environments (such as standard Python 3.x) shows that the multiline f-string works as expected.
This issue does not occur with simpler, single-line f-strings.
Ensuring no invisible characters or incorrect syntax in the code.
Code of Conduct
Yes, I agree
Formatted string literals cannot handle colon character in string literals
I'm using MicroPython v1.19.1-88-g74e33e714 on 2022-06-30; Raspberry Pi Pico W with RP2040
Running the following expression in the REPL:
print(f"foo {':'} bar")
results in this error:
<img width="322" alt="CleanShot 2022-07-05 at 22 55 26@2x" src="https://user-images.githubusercontent.com/1898161/177414906-b19ca3e6-232b-45d3-9873-bdde8f86121f.png">
Replacing the ':' by any other character fixes the "syntax error":
<img width="270" alt="CleanShot 2022-07-05 at 23 06 54@2x" src="https://user-images.githubusercontent.com/1898161/177416593-9fe3616f-04f4-4226-9440-fe17971ba703.png">
Running this in Python 3.9.2 works fine:
<img width="230" alt="CleanShot 2022-07-05 at 22 57 28@2x" src="https://user-images.githubusercontent.com/1898161/177415349-37795433-7e88-4532-b80a-4b0fd39a5328.png">