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
py: Error freezing a python source file with concatenated f-strings
Port, board and/or hardware
any
MicroPython version
1.23.0
Reproduction
This error happens if you freeze a python source file with code like what is seen below.
TEST1 = 'Test1'
TEST2 = 'Test2'
TEST3 = 'Test3'
print(
f'{TEST1}, '
f'{TEST2}, '
f'{TEST3}'
)
The error is "invalid syntax"
If I change the code to the following no errors occur.
TEST1 = 'Test1'
TEST2 = 'Test2'
TEST3 = 'Test3'
print(f'{TEST1}, {TEST2}, {TEST3}')
the error occurs because of the formatted text being on multiple lines. This is correct python syntax and I am not sure why it fails. If I do this there is no error.
print(
'Test1, '
'Test2, '
'Test3'
)
the problem only occurs when using the format specifier.
Expected behaviour
For the code to properly run.
Observed behaviour
Syntax Error occurs
Additional Information
I am not sure if there is an error if I am not freezing the file. For some reason I believe that it does still occur if I run the source file instead of freezing it.
Code of Conduct
Yes, I agree