Skip to content

Commit

Permalink
Update type annotations and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Feb 4, 2022
1 parent 5fc4ce9 commit bbbbf15
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions adafruit_bus_device/i2c_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from typing import Optional, Type
from types import TracebackType
from busio import I2C

try:
from circuitpython_typing import ReadableBuffer, WriteableBuffer
except ImportError:
from _typing import ReadableBuffer, WriteableBuffer
except ImportError:
pass

Expand Down Expand Up @@ -48,15 +53,17 @@ class I2CDevice:
device.write(bytes_read)
"""

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

self.i2c = i2c
self.device_address = device_address

if probe:
self.__probe_for_device()

def readinto(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
def readinto(
self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None
) -> None:
"""
Read into ``buf`` from the device. The number of bytes read will be the
length of ``buf``.
Expand All @@ -65,15 +72,17 @@ def readinto(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None)
as if ``buf[start:end]``. This will not cause an allocation like
``buf[start:end]`` will so it saves memory.
:param bytearray buffer: buffer to write into
:param ~WriteableBuffer buffer: buffer to write into
:param int start: Index to start writing at
:param int end: Index to write up to but not include; if None, use ``len(buf)``
"""
if end is None:
end = len(buf)
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)

def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
def write(
self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None
) -> None:
"""
Write the bytes from ``buffer`` to the device, then transmit a stop
bit.
Expand All @@ -82,7 +91,7 @@ def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
as if ``buffer[start:end]``. This will not cause an allocation like
``buffer[start:end]`` will so it saves memory.
:param bytearray buffer: buffer containing the bytes to write
:param ~ReadableBuffer buffer: buffer containing the bytes to write
:param int start: Index to start writing from
:param int end: Index to read up to but not include; if None, use ``len(buf)``
"""
Expand All @@ -93,14 +102,14 @@ def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
# pylint: disable-msg=too-many-arguments
def write_then_readinto(
self,
out_buffer: bytearray,
in_buffer: bytearray,
out_buffer: ReadableBuffer,
in_buffer: WriteableBuffer,
*,
out_start: int = 0,
out_end: Optional[int] = None,
in_start: int = 0,
in_end: Optional[int] = None
):
) -> None:
"""
Write the bytes from ``out_buffer`` to the device, then immediately
reads into ``in_buffer`` from the device. The number of bytes read
Expand All @@ -116,8 +125,8 @@ def write_then_readinto(
cause an allocation like ``in_buffer[in_start:in_end]`` will so
it saves memory.
:param bytearray out_buffer: buffer containing the bytes to write
:param bytearray in_buffer: buffer containing the bytes to read into
:param ~ReadableBuffer out_buffer: buffer containing the bytes to write
:param ~WriteableBuffer in_buffer: buffer containing the bytes to read into
:param int out_start: Index to start writing from
:param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
:param int in_start: Index to start writing at
Expand Down

0 comments on commit bbbbf15

Please sign in to comment.