From e0c3ab5f52892b181c7f720a2636ec34386a5a52 Mon Sep 17 00:00:00 2001 From: Randy Hudson Date: Mon, 21 Feb 2022 23:35:25 -0500 Subject: [PATCH 1/2] added ByteStream protocol --- circuitpython_typing/__init__.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/circuitpython_typing/__init__.py b/circuitpython_typing/__init__.py index 5817538..1dcb2b6 100644 --- a/circuitpython_typing/__init__.py +++ b/circuitpython_typing/__init__.py @@ -8,13 +8,13 @@ Types needed for type annotation that are not in `typing` -* Author(s): Alec Delaney, Dan Halbert +* Author(s): Alec Delaney, Dan Halbert, Randy Hudson """ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Typing.git" -from typing import Union +from typing import Union, Protocol, Optional from array import array ReadableBuffer = Union[bytes, bytearray, memoryview, array] @@ -31,3 +31,25 @@ * `memoryview` * `array.array` """ + + +class ByteStream(Protocol): + """Protocol for basic I/O operations on a byte stream. + Classes which implement this protocol include + * `io.BytesIO` + * `io.FileIO` (for a file open in binary mode) + * `busio.UART` + * `usb_cdc.Serial` + """ + + def read(self, count: Optional[int] = None, /) -> Optional[bytes]: + """Read `count` bytes from the stream. + If `count` bytes are not immediately available, + or if the parameter is not specified in the call, + the outcome is implementation-dependent. + """ + ... + + def write(self, buf: ReadableBuffer, /) -> Optional[int]: + """Write the bytes in `buf` to the stream.""" + ... From e09d572c0710fa83ced2b9c8b70959ead7ad2133 Mon Sep 17 00:00:00 2001 From: Randy Hudson Date: Tue, 22 Feb 2022 11:46:29 -0500 Subject: [PATCH 2/2] added ByteStream protocol, take 2 --- circuitpython_typing/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/circuitpython_typing/__init__.py b/circuitpython_typing/__init__.py index 1dcb2b6..7f922aa 100644 --- a/circuitpython_typing/__init__.py +++ b/circuitpython_typing/__init__.py @@ -43,13 +43,13 @@ class ByteStream(Protocol): """ def read(self, count: Optional[int] = None, /) -> Optional[bytes]: - """Read `count` bytes from the stream. - If `count` bytes are not immediately available, + """Read ``count`` bytes from the stream. + If ``count`` bytes are not immediately available, or if the parameter is not specified in the call, the outcome is implementation-dependent. """ ... def write(self, buf: ReadableBuffer, /) -> Optional[int]: - """Write the bytes in `buf` to the stream.""" + """Write the bytes in ``buf`` to the stream.""" ...