pxt-arcade icon indicating copy to clipboard operation
pxt-arcade copied to clipboard

Python -> TS type inference fails on array function arguments

Open riknoll opened this issue 1 year ago • 1 comments

Reported here: https://forum.makecode.com/t/using-normal-python-in-makecode-but-its-not-working/25746 and here: https://stackoverflow.com/questions/76394372/what-does-e-findidx-is-not-a-function-mean-in-minecraft-makecode-python

When switching from python to ts, our type inference fails on functions that take array parameters. For example, this Python:

def whatever(val):
    val[0] = 0

whatever([1])

converts to this ts:

function whatever(val: any) {
    val[0] = 0
}

whatever([1])

Note that the parameter type is any, which confuses the compiler so that it emits code that tries to access the index as it would on an object and not on an array. This throws an exception in browser and on hardware.

riknoll avatar Jan 10 '24 22:01 riknoll

a workaround for this is to give a type hint in Python

def whatever(val: List[number]):
    val[0] = 0

whatever([1])

...though we should definitely fix this bug as most folks wouldn't think to do that

riknoll avatar Jan 10 '24 22:01 riknoll