Skip to content

Commit 85e90eb

Browse files
authored
Merge pull request #2 from rimwolf-redux/bytestream
added ByteStream protocol
2 parents 819c51b + e09d572 commit 85e90eb

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

circuitpython_typing/__init__.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
Types needed for type annotation that are not in `typing`
99
1010
11-
* Author(s): Alec Delaney, Dan Halbert
11+
* Author(s): Alec Delaney, Dan Halbert, Randy Hudson
1212
"""
1313

1414
__version__ = "0.0.0-auto.0"
1515
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Typing.git"
1616

17-
from typing import Union
17+
from typing import Union, Protocol, Optional
1818
from array import array
1919

2020
ReadableBuffer = Union[bytes, bytearray, memoryview, array]
@@ -31,3 +31,25 @@
3131
* `memoryview`
3232
* `array.array`
3333
"""
34+
35+
36+
class ByteStream(Protocol):
37+
"""Protocol for basic I/O operations on a byte stream.
38+
Classes which implement this protocol include
39+
* `io.BytesIO`
40+
* `io.FileIO` (for a file open in binary mode)
41+
* `busio.UART`
42+
* `usb_cdc.Serial`
43+
"""
44+
45+
def read(self, count: Optional[int] = None, /) -> Optional[bytes]:
46+
"""Read ``count`` bytes from the stream.
47+
If ``count`` bytes are not immediately available,
48+
or if the parameter is not specified in the call,
49+
the outcome is implementation-dependent.
50+
"""
51+
...
52+
53+
def write(self, buf: ReadableBuffer, /) -> Optional[int]:
54+
"""Write the bytes in ``buf`` to the stream."""
55+
...

0 commit comments

Comments
 (0)