Josverl/micropython-stubs

array.array is missing __setitem__ and __getitem__

Closed this issue · 7 comments

mgsb commented

The array class is missing the set/get methods so that indexed accesses are allowed.

Hi Mark,

Yes , I noticed that problem as well while doing some other updates

I have added a quality check and a fix for that in micropython-stubber already:
Josverl/micropython-stubber@1741969
but am still going through other improvements and quality checks across ports and versions

I plan to release a set of new stubs somewhere this week.
any specific port that is relevant to you ?

mgsb commented

Thanks. I mostly use esp32-merged. I continue using stubs with pyright and lsp-mode in Emacs - great stuff!

@mgsb,
this seemingly simple fix took longer than expected as the cause turned out to be the changes in naming convention for the boards, in combination with the old names still being present in the files and folders in the repo.

It took me some time longer to find that, and clean it up.

as you mentioned you were using (a copy of) the esp32-merged the below is likely simpler
You can install the latest esp32 stubs using pip install -U micropython-esp32-stubs --target typings --no-user

mgsb commented

Thanks for looking at this, but I don't see any change(s):

mark@sam:/tmp$ pip install -U micropython-esp32-stubs --target typings --no-user --upgrade
Collecting micropython-esp32-stubs
Using cached micropython_esp32_stubs-1.21.0.post3-py3-none-any.whl (150 kB)
Collecting micropython-stdlib-stubs>=0.9.0
Using cached micropython_stdlib_stubs-1.0.0-py3-none-any.whl (121 kB)
Installing collected packages: micropython-stdlib-stubs, micropython-esp32-stubs
Successfully installed micropython-esp32-stubs-1.21.0.post3 micropython-stdlib-stubs-1.0.0

In the repo (after a pull), I only see the methods in the micropython-docs-stubs/array.pyi file.

I'll re-open this until we got it figured out.

The way this is implemented in the stubs is that array is a subclass of List
The published version has :

class array(List):
"""
Create array with elements of given type. Initial contents of the
array are given by *iterable*. If it is not provided, an empty
array is created.
"""
def extend(self, iterable) -> Incomplete:
"""
Append new elements as contained in *iterable* to the end of
array, growing it.
"""
...
def append(self, val) -> Incomplete:
"""
Append new element *val* to the end of array, growing it.
"""
...
def __init__(self, typecode, iterable: Optional[Any] = None) -> None: ...

You should see the same in the installed version.

I verify the correctness using this code snippet :

import array
NUM_LEDS = 10
ar = array.array("I", [0 for _ in range(NUM_LEDS)])
x = ar[3]
ar[3] = 0x12345678

If you have some code that fail , then we can

  • add it to the test to show the error
  • improve the stubs so that the error is resolved/
mgsb commented

Sorry - I see it now. Not sure what I did wrong the other day - possibly did not restart the LSP server? I am confused why the stubs/ directory has a different version of array.pyi than the publish directory. I guess a rule is to not look in the stubs/ directory as there is post-processing happening into the publish/ directory?
I appreciate your support - thanks!

Yes, the published stubs have the highest quality.
The others are only tested to not have syntax or semantic errors.

It's all about the order and combination of different info from different sources.
It has grown over time, so it's not obvious.