python-editor-v3 icon indicating copy to clipboard operation
python-editor-v3 copied to clipboard

SyntaxError: constant must be an integer

Open Thesnake66six opened this issue 2 years ago • 2 comments
trafficstars

Although code runs fine on the actual microbit, it throws this error in the simulator.

from micropython import const

MY_CONST = const(0.5)

Thesnake66six avatar Dec 11 '22 18:12 Thesnake66six

Thanks for the report.

This seems to be a difference between MicroPython for the micro:bit V1 board version (where it works) and MicroPython for the micro:bit V2 where it does not. The simulator is based on MicroPython for micro:bit V2.

I'll get some input on why this difference exists and follow up here.

microbit-matt-hillsdon avatar Dec 12 '22 09:12 microbit-matt-hillsdon

For V1 const isn't doing anything, it's simply returning its argument. This is unlikely to change at this point due to code size and implementation effort constraints. If you're free to change this code then you can simply remove the const call.

For V2 we're using MicroPython 1.18.0 where using const is optimising the integer constants and intentionally failing for non-integer values.

Coincidentally, upstream MicroPython 1.19.1 now has support for more types of constant. We'll plan an upgrade in future which will align the behaviour (though V1 will still lack the optimisation).

microbit-matt-hillsdon avatar Dec 12 '22 11:12 microbit-matt-hillsdon