micropy-cli: TypeError while generating stubs from board via USB

Describe the bug

micropy fails to generate stubs from a PicoW board running Pimoroni Micropython, when no other stubs exist.

Expected Behavior

Micropy generates stubs successfully.

Current Behavior

Micropy produces a TypeError and doesn’t generate the stubs.

Steps to Reproduce

  1. Make sure you have no stubs installed - e.g. rm -rf ~/.micropy
  2. Connect a Raspberry Pi Pico W board via USB, holding bootsel
  3. Copy the Pimoroni Micropython 1.19.16 image for Pico W to the board, from Releases page
  4. Run micropy via USB, with something like micropy stubs create /dev/cu.usbmodem11201

Possible Solution

For me, adding str around name in the line "/".join([package.repo_name, name]) in resolve_package function of repo.py fixed the issue.

    def resolve_package(self, name: str) -> StubRepositoryPackage:
        """Resolve a package name to a package path.

        Args:
            name: Package name.

        Returns:
            Package location.

        Throws:
            StubNotFound: When package cannot be resolved.

        """
        for package in self.search(str(name)):
            if package.match_exact(name) or package.match_exact(
                "/".join([package.repo_name, str(name)])
            ):
                return package
            latest = self.latest_for_package(package)
            if latest and latest.name == name:
                return latest
        raise exc.StubNotFound(name)

Once the stubs have generated once with this change, the unmodified code works fine - seems to be something about having no stubs, or maybe not having an existing version of the same-named stubs?

Logs

The last section of output from micropy stubs create /dev/cu.usbmodem11201 - everything before this looks normal:

MicroPy  Pyboard  Reading /stubs/micropython-rp2-1_19_1/websocket.py: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| [329/329 @ 320B/s]
MicroPy  Pyboard  Reading /stubs/micropython-rp2-1_19_1/zlib.py: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| [273/273 @ 269B/s]
MicroPy  Copied Stubs: stubs
Traceback (most recent call last):
  File "/opt/homebrew/bin/micropy", line 8, in <module>
    sys.exit(cli())
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/click/decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/cli.py", line 306, in create
    return mpy.create_stubs(port, verbose=verbose)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/main.py", line 143, in create_stubs
    stub = self.stubs.add(stub_path)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/stubs/stubs.py", line 336, in add
    return self._load(stub_source, copy_to=dest)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/stubs/stubs.py", line 98, in _load
    with stub_source.ready() as src_path:
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/stubs/source.py", line 77, in ready
    path = reduce(reducer, self.locators, location or self.location)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/stubs/source.py", line 52, in _do_locate
    response = locator.prepare(path)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/stubs/source.py", line 138, in prepare
    source = self.repo.resolve_package(location)
  File "/opt/homebrew/lib/python3.10/site-packages/micropy/stubs/repo.py", line 134, in resolve_package
    "/".join([package.repo_name, name])
TypeError: sequence item 1: expected str instance, PosixPath found

micropy.log

Context (Environment)

  • OS: macOS Ventura 13.2.1, M1 processor
  • Micropy Version: 4.1.0
  • Python Version: Python 3.10.6
  • VSCode Version: 1.76.0

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Just realised I’d chopped off a bit too much - there’s also a “Failed to create the report” message earlier in the output:

MicroPy  Pyboard  Stub module: urandom                   to file: /stubs/micropython-v1_19_1-rp2/urandom.py                              mem:116656
MicroPy  Pyboard  Stub module: ure                       to file: /stubs/micropython-v1_19_1-rp2/ure.py                                  mem:116656
MicroPy  Pyboard  Stub module: urequests                 to file: /stubs/micropython-v1_19_1-rp2/urequests.py                            mem:116656
MicroPy  Pyboard  Stub module: urllib/urequest           to file: /stubs/micropython-v1_19_1-rp2/urllib/urequest.py             Skip module: urllib.urequest           Module not found.
MicroPy  Pyboard  Stub module: uselect                   to file: /stubs/micropython-v1_19_1-rp2/uselect.py                              mem:116640
MicroPy  Pyboard  Stub module: usocket                   to file: /stubs/micropython-v1_19_1-rp2/usocket.py                              mem:116640
MicroPy  Pyboard  Stub module: ussl                      to file: /stubs/micropython-v1_19_1-rp2/ussl.py                                 mem:116640
MicroPy  Pyboard  Stub module: ustruct                   to file: /stubs/micropython-v1_19_1-rp2/ustruct.py                              mem:116640
MicroPy  Pyboard  Stub module: usys                      to file: /stubs/micropython-v1_19_1-rp2/usys.py                                 mem:116640
MicroPy  Pyboard  Stub module: utelnetserver             to file: /stubs/micropython-v1_19_1-rp2/utelnetserver.py               Skip module: utelnetserver             Module not found.
MicroPy  Pyboard  Stub module: utime                     to file: /stubs/micropython-v1_19_1-rp2/utime.py                                mem:116624
MicroPy  Pyboard  Stub module: utimeq                    to file: /stubs/micropython-v1_19_1-rp2/utimeq.py                      Skip module: utimeq                    Module not found.
MicroPy  Pyboard  Stub module: uwebsocket                to file: /stubs/micropython-v1_19_1-rp2/uwebsocket.py                           mem:116608
MicroPy  Pyboard  Stub module: uzlib                     to file: /stubs/micropython-v1_19_1-rp2/uzlib.py                                mem:116608
MicroPy  Pyboard  Stub module: version                   to file: /stubs/micropython-v1_19_1-rp2/version.py                              mem:116608
MicroPy  Pyboard  Stub module: websocket                 to file: /stubs/micropython-v1_19_1-rp2/websocket.py                            mem:116528
MicroPy  Pyboard  Stub module: websocket_helper          to file: /stubs/micropython-v1_19_1-rp2/websocket_helper.py            Skip module: websocket_helper          Module not found.
MicroPy  Pyboard  Stub module: wipy                      to file: /stubs/micropython-v1_19_1-rp2/wipy.py                        Skip module: wipy                      Module not found.
MicroPy  Pyboard  Stub module: writer                    to file: /stubs/micropython-v1_19_1-rp2/writer.py                      Skip module: writer                    Module not found.
MicroPy  Pyboard  Stub module: xpt2046                   to file: /stubs/micropython-v1_19_1-rp2/xpt2046.py                     Skip module: xpt2046                   Module not found.
MicroPy  Pyboard  Stub module: ymodem                    to file: /stubs/micropython-v1_19_1-rp2/ymodem.py                      Skip module: ymodem                    Module not found.
MicroPy  Pyboard  Stub module: zephyr                    to file: /stubs/micropython-v1_19_1-rp2/zephyr.py                      Skip module: zephyr                    Module not found.
MicroPy  Pyboard  Stub module: zlib                      to file: /stubs/micropython-v1_19_1-rp2/zlib.py                                 mem:116464
MicroPy  Pyboard  Created stubs for 48 modules on board micropython-v1.19.1-rp2
MicroPy  Pyboard  Path: /stubs/micropython-v1_19_1-rp2
MicroPy  Pyboard  Failed to create the report.
MicroPy  ✔ Done!
MicroPy  Copying stubs...
MicroPy  Pyboard  Reading /stubs/micropython-v1_19_1-rp2/_boot_fat.py: 100%|██████████████| [172/172 @ 279B/s]
MicroPy  Pyboard  Reading /stubs/micropython-v1_19_1-rp2/_onewire.py: 100%|███████████████| [309/309 @ 302B/s]

I grabbed a copy of the files that had been copied to the temp directory at the point the script fails, interestingly the last few are 0 bytes, these are also the files that display “[0.00/0.00 @ ?B/s]” in the micropy output. The file names also stop before the end of the list above - there’s nothing after onewire, even though there are other packages listed in the stub creation output from micropy (e.g. uzlib, version, websocket etc. Is there any chance it’s running out of RAM/Flash on the board?

-rw-r--r--   1 rebeam  staff   344B Mar 23 10:33 _boot_fat.py
-rw-r--r--   1 rebeam  staff   618B Mar 23 10:33 _onewire.py
-rw-r--r--   1 rebeam  staff   1.9K Mar 23 10:33 _rp2.py
-rw-r--r--   1 rebeam  staff   849B Mar 23 10:33 _thread.py
-rw-r--r--   1 rebeam  staff   732B Mar 23 10:33 _uasyncio.py
-rw-r--r--   1 rebeam  staff   542B Mar 23 10:33 adcfft.py
-rw-r--r--   1 rebeam  staff   536B Mar 23 10:33 array.py
-rw-r--r--   1 rebeam  staff   579B Mar 23 10:33 binascii.py
-rw-r--r--   1 rebeam  staff   1.6K Mar 23 10:33 breakout_as7262.py
-rw-r--r--   1 rebeam  staff   1.0K Mar 23 10:33 breakout_bh1745.py
-rw-r--r--   1 rebeam  staff   1.2K Mar 23 10:33 breakout_bme280.py
-rw-r--r--   1 rebeam  staff   1.4K Mar 23 10:33 breakout_bme68x.py
-rw-r--r--   1 rebeam  staff   1.3K Mar 23 10:34 breakout_bmp280.py
-rw-r--r--   1 rebeam  staff   933B Mar 23 10:34 breakout_dotmatrix.py
-rw-r--r--   1 rebeam  staff   1.1K Mar 23 10:34 breakout_encoder.py
-rw-r--r--   1 rebeam  staff   769B Mar 23 10:34 breakout_icp10125.py
-rw-r--r--   1 rebeam  staff   2.0K Mar 23 10:34 breakout_ioexpander.py
-rw-r--r--   1 rebeam  staff   1.5K Mar 23 10:34 breakout_ltr559.py
-rw-r--r--   1 rebeam  staff   678B Mar 23 10:34 breakout_matrix11x7.py
-rw-r--r--   1 rebeam  staff   1.5K Mar 23 10:34 breakout_mics6814.py
-rw-r--r--   1 rebeam  staff   2.5K Mar 23 10:34 breakout_msa301.py
-rw-r--r--   1 rebeam  staff   1.1K Mar 23 10:34 breakout_paa5100.py
-rw-r--r--   1 rebeam  staff   1.1K Mar 23 10:34 breakout_pmw3901.py
-rw-r--r--   1 rebeam  staff   1.1K Mar 23 10:34 breakout_potentiometer.py
-rw-r--r--   1 rebeam  staff   681B Mar 23 10:34 breakout_rgbmatrix5x5.py
-rw-r--r--   1 rebeam  staff   4.2K Mar 23 10:34 breakout_rtc.py
-rw-r--r--   1 rebeam  staff   808B Mar 23 10:34 breakout_scd41.py
-rw-r--r--   1 rebeam  staff   1.1K Mar 23 10:34 breakout_sgp30.py
-rw-r--r--   1 rebeam  staff   1.1K Mar 23 10:34 breakout_trackball.py
-rw-r--r--   1 rebeam  staff   1.7K Mar 23 10:34 breakout_vl53l5cx.py
-rw-r--r--   1 rebeam  staff   781B Mar 23 10:34 cmath.py
-rw-r--r--   1 rebeam  staff   1.3K Mar 23 10:34 collections.py
-rw-r--r--   1 rebeam  staff   540B Mar 23 10:35 cryptolib.py
-rw-r--r--   1 rebeam  staff   1.0K Mar 23 10:35 dht.py
-rw-r--r--   1 rebeam  staff   781B Mar 23 10:35 ds18x20.py
-rw-r--r--   1 rebeam  staff   1.3K Mar 23 10:35 encoder.py
-rw-r--r--   1 rebeam  staff   939B Mar 23 10:35 errno.py
-rw-r--r--   1 rebeam  staff   1.3K Mar 23 10:35 framebuf.py
-rw-r--r--   1 rebeam  staff   665B Mar 23 10:35 gc.py
-rw-r--r--   1 rebeam  staff   4.1K Mar 23 10:35 gfx_pack.py
-rw-r--r--   1 rebeam  staff   734B Mar 23 10:35 hashlib.py
-rw-r--r--   1 rebeam  staff   479B Mar 23 10:35 heapq.py
-rw-r--r--   1 rebeam  staff   1.2K Mar 23 10:35 hub75.py
-rw-r--r--   1 rebeam  staff   5.0K Mar 23 10:35 interstate75.py
-rw-r--r--   1 rebeam  staff   1.6K Mar 23 10:35 io.py
-rw-r--r--   1 rebeam  staff   859B Mar 23 10:35 jpegdec.py
-rw-r--r--   1 rebeam  staff   513B Mar 23 10:35 json.py
-rw-r--r--   1 rebeam  staff   1.9K Mar 23 10:35 lwip.py
-rw-r--r--   1 rebeam  staff    10K Mar 23 10:35 machine.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:35 math.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 micropython.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 mip.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 motor.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 neopixel.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 network.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 ntptime.py
-rw-r--r--   1 rebeam  staff     0B Mar 23 10:36 onewire.py

@BradenM Ah that’s great - I’ll stand by for the prerelease, thanks!