Skip to content

Commit 51460cf

Browse files
authored
Merge pull request #11 from tekktrik/doc/add-typing
Add type annotations
2 parents 990bbda + 707be9d commit 51460cf

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

adafruit_debug_i2c.py

+42-23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
2222
"""
2323

24+
try:
25+
from typing import Optional, Type, List
26+
from types import TracebackType
27+
from circuitpython_typing import WriteableBuffer, ReadableBuffer
28+
from busio import I2C
29+
except ImportError:
30+
pass
31+
2432
__version__ = "0.0.0-auto.0"
2533
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debug_I2C.git"
2634

@@ -57,35 +65,47 @@ class DebugI2C:
5765
5866
"""
5967

60-
def __init__(self, i2c):
68+
def __init__(self, i2c: I2C) -> None:
6169
self._i2c = i2c
6270
if hasattr(self._i2c, "writeto_then_readfrom"):
6371
self.writeto_then_readfrom = self._writeto_then_readfrom
6472

65-
def __enter__(self):
73+
def __enter__(self) -> I2C:
6674
"""
6775
No-op used in Context Managers.
6876
"""
6977
return self._i2c.__enter__()
7078

71-
def __exit__(self, exc_type, exc_val, exc_tb):
79+
def __exit__(
80+
self,
81+
exc_type: Optional[Type[type]],
82+
exc_val: Optional[BaseException],
83+
exc_tb: Optional[TracebackType],
84+
) -> None:
7285
"""
7386
Automatically deinitialises the hardware on context exit.
7487
"""
7588
return self._i2c.__exit__(exc_type, exc_val, exc_tb)
7689

77-
def deinit(self):
90+
def deinit(self) -> None:
7891
"""
7992
Releases control of the underlying I2C hardware so other classes can use it.
8093
"""
8194
return self._i2c.deinit()
8295

83-
def readfrom_into(self, address, buffer, *args, start=0, end=None):
96+
def readfrom_into(
97+
self,
98+
address: int,
99+
buffer: WriteableBuffer,
100+
*args,
101+
start: int = 0,
102+
end: Optional[int] = None
103+
) -> None:
84104
"""
85105
Debug version of ``readfrom_into`` that prints the buffer after reading.
86106
87107
:param int address: 7-bit device address
88-
:param bytearray buffer: buffer to write into
108+
:param ~WritableBuffer buffer: buffer to write into
89109
:param int start: Index to start writing at
90110
:param int end: Index to write up to but not include
91111
@@ -95,7 +115,7 @@ def readfrom_into(self, address, buffer, *args, start=0, end=None):
95115
in_buffer_str = ", ".join([hex(i) for i in buffer])
96116
print("\tI2CREAD @ {} ::".format(hex(address)), in_buffer_str)
97117

98-
def scan(self):
118+
def scan(self) -> List[int]:
99119
"""
100120
Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that
101121
respond.
@@ -105,7 +125,7 @@ def scan(self):
105125
"""
106126
return self._i2c.scan()
107127

108-
def try_lock(self):
128+
def try_lock(self) -> bool:
109129
"""
110130
Attempts to grab the I2C lock. Returns True on success.
111131
@@ -114,18 +134,18 @@ def try_lock(self):
114134
"""
115135
return self._i2c.try_lock()
116136

117-
def unlock(self):
137+
def unlock(self) -> None:
118138
"""
119139
Releases the I2C lock.
120140
"""
121141
return self._i2c.unlock()
122142

123-
def writeto(self, address, buffer, *args, **kwargs):
143+
def writeto(self, address: int, buffer: ReadableBuffer, *args, **kwargs) -> None:
124144
"""
125145
Debug version of ``write`` that prints the buffer before sending.
126146
127147
:param int address: 7-bit device address
128-
:param bytearray buffer: buffer containing the bytes to write
148+
:param ~ReadableBuffer buffer: buffer containing the bytes to write
129149
:param int start: Index to start writing from
130150
:param int end: Index to read up to but not include
131151
:param bool stop: If true, output an I2C stop condition after the
@@ -138,23 +158,22 @@ def writeto(self, address, buffer, *args, **kwargs):
138158

139159
def _writeto_then_readfrom(
140160
self,
141-
address,
142-
buffer_out,
143-
buffer_in,
161+
address: int,
162+
buffer_out: ReadableBuffer,
163+
buffer_in: WriteableBuffer,
144164
*args,
145-
out_start=0,
146-
out_end=None,
147-
in_start=0,
148-
in_end=None
149-
):
165+
out_start: int = 0,
166+
out_end: Optional[int] = None,
167+
in_start: int = 0,
168+
in_end: Optional[int] = None
169+
) -> None:
150170
"""
151171
Debug version of ``write_readinto`` that prints the ``buffer_out`` before writing and the
152172
``buffer_in`` after reading.
153173
154-
:TODO Verify parameter documentation is accurate
155-
:param address: 7-bit device address
156-
:param bytearray buffer_out: Write out the data in this buffer
157-
:param bytearray buffer_in: Read data into this buffer
174+
:param int address: 7-bit device address
175+
:param ~ReadableBuffer buffer_out: Write out the data in this buffer
176+
:param ~WriteableBuffer buffer_in: Read data into this buffer
158177
:param int out_start: Start of the slice of buffer_out to write out:
159178
``buffer_out[out_start:out_end]``
160179
:param int out_end: End of the slice; this index is not included. Defaults to

0 commit comments

Comments
 (0)