From df162a43b617553a9e306418e9a30a8895b62824 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Sep 2025 13:47:47 +0000 Subject: [PATCH 1/2] Initial plan From 8c90d2d9effd2b26e0073524866d8ccd1faa69ed Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Sep 2025 13:59:55 +0000 Subject: [PATCH 2/2] Add _WriteStream protocol for MicroPython Buffer write methods Co-authored-by: Josverl <981654+Josverl@users.noreply.github.com> --- reference/_mpy_shed/__init__.pyi | 30 +++++++++++++++ reference/_mpy_shed/buffer_mp.pyi | 64 ++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/reference/_mpy_shed/__init__.pyi b/reference/_mpy_shed/__init__.pyi index 060a2fe43..ab21106c6 100644 --- a/reference/_mpy_shed/__init__.pyi +++ b/reference/_mpy_shed/__init__.pyi @@ -34,6 +34,36 @@ from .io_mp import _RawIOBase as _RawIOBase from .io_mp import _TextIOBase as _TextIOBase from .io_mp import open as open from .IRQs import _IRQ +from .neopixelbase import _NeoPixelBase as _NeoPixelBase +from .blockdevice import ( + _BlockDeviceProtocol as _BlockDeviceProtocol, + _OldAbstractBlockDev, + _OldAbstractReadOnlyBlockDev, +) +from .buffer_mp import ( + AnyReadableBuf as AnyReadableBuf, + AnyWritableBuf as AnyWritableBuf, + _WriteStream as _WriteStream, +) + +from .io_mp import ( + BytesIO as BytesIO, + FileIO as FileIO, + IncrementalNewlineDecoder as IncrementalNewlineDecoder, + StringIO as StringIO, + TextIOWrapper as TextIOWrapper, + IOBase_mp as IOBase_mp, + _BufferedIOBase, + _IOBase, + _RawIOBase, + _TextIOBase, + open as open, +) + +from .time_mp import _TimeTuple as _TimeTuple +from .pathlike import PathLike as PathLike + +from .mp_implementation import _mp_implementation as _mp_implementation from .mp_available import mp_available as mp_available from .mp_implementation import _mp_implementation as _mp_implementation from .neopixelbase import _NeoPixelBase as _NeoPixelBase diff --git a/reference/_mpy_shed/buffer_mp.pyi b/reference/_mpy_shed/buffer_mp.pyi index a1e3846b2..2dd5dc28c 100644 --- a/reference/_mpy_shed/buffer_mp.pyi +++ b/reference/_mpy_shed/buffer_mp.pyi @@ -1,8 +1,70 @@ from _typeshed import Incomplete, structseq, AnyStr_co -from typing_extensions import TypeAlias, TypeVar +from typing_extensions import TypeAlias, TypeVar, Protocol +from typing import overload from array import array # ------------------------------------------------------------------------------------ # TODO: need some to allow string to be passed in : uart_1.write("hello") AnyReadableBuf: TypeAlias = bytearray | array | memoryview | bytes | Incomplete AnyWritableBuf: TypeAlias = bytearray | array | memoryview | Incomplete + + +# ------------------------------------------------------------------------------------ +# MicroPython Buffer Protocol +# See: https://github.com/micropython/micropython/pull/17938#discussion_r2329804709 +# +# This protocol defines the extended write method that supports: +# - write(buf) - basic write +# - write(buf, max_len) - write with maximum length +# - write(buf, off, max_len) - write with offset and maximum length + + +class _WriteStream(Protocol): + """ + Protocol for MicroPython streams that support the extended write signature + with optional offset and max_len parameters. + + This is commonly used in MicroPython's IO streams, sockets, and serial interfaces. + """ + + @overload + def write(self, buf: AnyReadableBuf, /) -> int | None: + """ + Write the buffer of bytes to the stream. + + Args: + buf: Buffer to write + + Returns: + Number of bytes written, or None if non-blocking and would block + """ + ... + + @overload + def write(self, buf: AnyReadableBuf, max_len: int, /) -> int | None: + """ + Write up to max_len bytes from the buffer to the stream. + + Args: + buf: Buffer to write + max_len: Maximum number of bytes to write + + Returns: + Number of bytes written, or None if non-blocking and would block + """ + ... + + @overload + def write(self, buf: AnyReadableBuf, off: int, max_len: int, /) -> int | None: + """ + Write up to max_len bytes from the buffer starting at offset off to the stream. + + Args: + buf: Buffer to write + off: Offset into the buffer to start writing from + max_len: Maximum number of bytes to write + + Returns: + Number of bytes written, or None if non-blocking and would block + """ + ...