microsoft / microsoft/pxt-microbit
Python negative indices do not work
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 804
- Forks
- 721
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 2
Description
Python negative indices do not work
Code example:
myList = [1, 2, 3]
basic.showNumber(myList[-1])
Expected behaviour:
The number '3' to be displayed.
Observed behaviour:
The character '?' is displayed.
Independent of browser/micro:bit version
The problem seems to be that the Python->JavaScript cross-compiler just carries over the negative index, and this is not supported in JavaScript (although the JS behaviour in this case is ... interesting)
Would it be possible to convert a negative index from Python into JavaScript as ".length - X", rather than just -X?
e.g.,
let myList = [1, 2, 3]
basic.showNumber(myList[myList.length - 1])
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the Python-to-JavaScript cross-compiler in the pxt-microbit repository and trace how list indices are emitted. Reproduce the provided myList[-1] example, then verify that the generated program displays 3 and that positive indexing remains unchanged. Add or update regression coverage wherever compiler behavior is tested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, python, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100