Skip to content

Commit bbbbf15

Browse files
committed
Update type annotations and docstrings
1 parent 5fc4ce9 commit bbbbf15

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

adafruit_bus_device/i2c_device.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
from typing import Optional, Type
1212
from types import TracebackType
1313
from busio import I2C
14+
15+
try:
16+
from circuitpython_typing import ReadableBuffer, WriteableBuffer
17+
except ImportError:
18+
from _typing import ReadableBuffer, WriteableBuffer
1419
except ImportError:
1520
pass
1621

@@ -48,15 +53,17 @@ class I2CDevice:
4853
device.write(bytes_read)
4954
"""
5055

51-
def __init__(self, i2c: I2C, device_address: int, probe: bool = True):
56+
def __init__(self, i2c: I2C, device_address: int, probe: bool = True) -> None:
5257

5358
self.i2c = i2c
5459
self.device_address = device_address
5560

5661
if probe:
5762
self.__probe_for_device()
5863

59-
def readinto(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
64+
def readinto(
65+
self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None
66+
) -> None:
6067
"""
6168
Read into ``buf`` from the device. The number of bytes read will be the
6269
length of ``buf``.
@@ -65,15 +72,17 @@ def readinto(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None)
6572
as if ``buf[start:end]``. This will not cause an allocation like
6673
``buf[start:end]`` will so it saves memory.
6774
68-
:param bytearray buffer: buffer to write into
75+
:param ~WriteableBuffer buffer: buffer to write into
6976
:param int start: Index to start writing at
7077
:param int end: Index to write up to but not include; if None, use ``len(buf)``
7178
"""
7279
if end is None:
7380
end = len(buf)
7481
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
7582

76-
def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
83+
def write(
84+
self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None
85+
) -> None:
7786
"""
7887
Write the bytes from ``buffer`` to the device, then transmit a stop
7988
bit.
@@ -82,7 +91,7 @@ def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
8291
as if ``buffer[start:end]``. This will not cause an allocation like
8392
``buffer[start:end]`` will so it saves memory.
8493
85-
:param bytearray buffer: buffer containing the bytes to write
94+
:param ~ReadableBuffer buffer: buffer containing the bytes to write
8695
:param int start: Index to start writing from
8796
:param int end: Index to read up to but not include; if None, use ``len(buf)``
8897
"""
@@ -93,14 +102,14 @@ def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
93102
# pylint: disable-msg=too-many-arguments
94103
def write_then_readinto(
95104
self,
96-
out_buffer: bytearray,
97-
in_buffer: bytearray,
105+
out_buffer: ReadableBuffer,
106+
in_buffer: WriteableBuffer,
98107
*,
99108
out_start: int = 0,
100109
out_end: Optional[int] = None,
101110
in_start: int = 0,
102111
in_end: Optional[int] = None
103-
):
112+
) -> None:
104113
"""
105114
Write the bytes from ``out_buffer`` to the device, then immediately
106115
reads into ``in_buffer`` from the device. The number of bytes read
@@ -116,8 +125,8 @@ def write_then_readinto(
116125
cause an allocation like ``in_buffer[in_start:in_end]`` will so
117126
it saves memory.
118127
119-
:param bytearray out_buffer: buffer containing the bytes to write
120-
:param bytearray in_buffer: buffer containing the bytes to read into
128+
:param ~ReadableBuffer out_buffer: buffer containing the bytes to write
129+
:param ~WriteableBuffer in_buffer: buffer containing the bytes to read into
121130
:param int out_start: Index to start writing from
122131
:param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
123132
:param int in_start: Index to start writing at

0 commit comments

Comments
 (0)