paulober / Pico-W-Stub

MicroPython stubs for the Raspberry Pi Pico W
Apache License 2.0
10 stars 3 forks source link

wrong type in stubs (machine.pyi -> readline()) #1

Open jcernato opened 1 year ago

jcernato commented 1 year ago

When playing around with UART on the pico I noticed that UART.readline() returns a 'bytes' object (or None) instead of a String. VSCode consequently gives an error when calling .decode() on the returnvalue.

pseudocode: device = UART(0, 9600) rec = device.readline() if rec is None: continue else: rec.decode() <- 'Cannot access member "decode" for type "str"'

paulober commented 1 year ago

Thanks for reporting this. I'll look into it.

paulober commented 1 year ago

@jcernato Ok, i've tested it on my machine at all sems working as expected.

import machine

u = machine.UART(0, 115200)
u.readline()

uartreadline Also the file you mentioned machine.pyi contains str|None as return type for UART.readline().

Can you please check your extension, vscode and Stub version. You can get the version of your stubs by checking the file inside your workspace > .vscode > Pico-W-Stub called version.json

jcernato commented 1 year ago

OK, I just figured the issue is a little more subtle as the return type of readline() depends on the class the method is bound to. BTW this is not micropython specific but a general python thing and the following example code was executed on my ubuntu host machine but works identically on the pico:

Reading a file (TextIOWrapper):

.>>> f = open('/tmp/testfile', 'r') .>>> type(f.readline()) <class 'str'>

Reading from (Serial):

.>>> import serial .>>> s = serial.Serial('/dev/ttyACM0', 115200) .>>> type(s.readline()) <class 'bytes'>

The io.pyi (~/.vscode/extensions/ms-python.vscode-pylance-2022.10.10/dist/typeshed-fallback/stdlib/io.pyi on my machine) handles this correctly but I am not acquainted enough with vscode stubs to understand how it does it. machine.pyi should also be able to handle different return types.