microsoft / microsoft/pxt-microbit

Python negative indices do not work

Open
#6,338 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P2 python
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.