Skip to content

added ByteStream protocol #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions circuitpython_typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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."""
...