Skip to content

Commit 9a5039e

Browse files
authored
Merge pull request #75 from tekktrik/doc/add-typing
Add type annotations, update documentation
2 parents fb851ff + 5355337 commit 9a5039e

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

.pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ confidence=
5555
# no Warning level messages displayed, use"--disable=all --enable=classes
5656
# --disable=W"
5757
# disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
58-
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation,unspecified-encoding
58+
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation,unspecified-encoding,duplicate-code
5959

6060
# Enable the message, report, category or checker with the given id(s). You can
6161
# either give multiple identifier separated by comma (,) or put this option

adafruit_bus_device/i2c_device.py

+38-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
====================================================
88
"""
99

10+
try:
11+
from typing import Optional, Type
12+
from types import TracebackType
13+
from busio import I2C
14+
15+
try:
16+
from circuitpython_typing import ReadableBuffer, WriteableBuffer
17+
except ImportError:
18+
from _typing import ReadableBuffer, WriteableBuffer
19+
except ImportError:
20+
pass
21+
1022
__version__ = "0.0.0-auto.0"
1123
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
1224

@@ -41,15 +53,17 @@ class I2CDevice:
4153
device.write(bytes_read)
4254
"""
4355

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

4658
self.i2c = i2c
4759
self.device_address = device_address
4860

4961
if probe:
5062
self.__probe_for_device()
5163

52-
def readinto(self, buf, *, start=0, end=None):
64+
def readinto(
65+
self, buf: WriteableBuffer, *, start: int = 0, end: Optional[int] = None
66+
) -> None:
5367
"""
5468
Read into ``buf`` from the device. The number of bytes read will be the
5569
length of ``buf``.
@@ -58,15 +72,17 @@ def readinto(self, buf, *, start=0, end=None):
5872
as if ``buf[start:end]``. This will not cause an allocation like
5973
``buf[start:end]`` will so it saves memory.
6074
61-
:param bytearray buffer: buffer to write into
75+
:param ~WriteableBuffer buffer: buffer to write into
6276
:param int start: Index to start writing at
6377
:param int end: Index to write up to but not include; if None, use ``len(buf)``
6478
"""
6579
if end is None:
6680
end = len(buf)
6781
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
6882

69-
def write(self, buf, *, start=0, end=None):
83+
def write(
84+
self, buf: ReadableBuffer, *, start: int = 0, end: Optional[int] = None
85+
) -> None:
7086
"""
7187
Write the bytes from ``buffer`` to the device, then transmit a stop
7288
bit.
@@ -75,7 +91,7 @@ def write(self, buf, *, start=0, end=None):
7591
as if ``buffer[start:end]``. This will not cause an allocation like
7692
``buffer[start:end]`` will so it saves memory.
7793
78-
:param bytearray buffer: buffer containing the bytes to write
94+
:param ~ReadableBuffer buffer: buffer containing the bytes to write
7995
:param int start: Index to start writing from
8096
:param int end: Index to read up to but not include; if None, use ``len(buf)``
8197
"""
@@ -86,14 +102,14 @@ def write(self, buf, *, start=0, end=None):
86102
# pylint: disable-msg=too-many-arguments
87103
def write_then_readinto(
88104
self,
89-
out_buffer,
90-
in_buffer,
105+
out_buffer: ReadableBuffer,
106+
in_buffer: WriteableBuffer,
91107
*,
92-
out_start=0,
93-
out_end=None,
94-
in_start=0,
95-
in_end=None
96-
):
108+
out_start: int = 0,
109+
out_end: Optional[int] = None,
110+
in_start: int = 0,
111+
in_end: Optional[int] = None
112+
) -> None:
97113
"""
98114
Write the bytes from ``out_buffer`` to the device, then immediately
99115
reads into ``in_buffer`` from the device. The number of bytes read
@@ -109,8 +125,8 @@ def write_then_readinto(
109125
cause an allocation like ``in_buffer[in_start:in_end]`` will so
110126
it saves memory.
111127
112-
:param bytearray out_buffer: buffer containing the bytes to write
113-
: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
114130
:param int out_start: Index to start writing from
115131
:param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
116132
:param int in_start: Index to start writing at
@@ -133,16 +149,21 @@ def write_then_readinto(
133149

134150
# pylint: enable-msg=too-many-arguments
135151

136-
def __enter__(self):
152+
def __enter__(self) -> "I2CDevice":
137153
while not self.i2c.try_lock():
138154
pass
139155
return self
140156

141-
def __exit__(self, exc_type, exc_val, exc_tb):
157+
def __exit__(
158+
self,
159+
exc_type: Optional[Type[type]],
160+
exc_val: Optional[BaseException],
161+
exc_tb: Optional[TracebackType],
162+
) -> bool:
142163
self.i2c.unlock()
143164
return False
144165

145-
def __probe_for_device(self):
166+
def __probe_for_device(self) -> None:
146167
"""
147168
Try to read a byte from an address,
148169
if you get an OSError it means the device is not there

adafruit_bus_device/spi_device.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
====================================================
1010
"""
1111

12+
try:
13+
from typing import Optional, Type
14+
from types import TracebackType
15+
from busio import SPI
16+
from digitalio import DigitalInOut
17+
except ImportError:
18+
pass
19+
1220
__version__ = "0.0.0-auto.0"
1321
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
1422

@@ -23,6 +31,9 @@ class SPIDevice:
2331
DigitalInOut API.
2432
:param bool cs_active_value: Set to true if your device requires CS to be active high.
2533
Defaults to false.
34+
:param int baudrate: The SPI baudrate
35+
:param int polarity: The SPI polarity
36+
:param int phase: The SPI phase
2637
:param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high.
2738
(Used for SD cards.)
2839
@@ -54,15 +65,15 @@ class SPIDevice:
5465

5566
def __init__(
5667
self,
57-
spi,
58-
chip_select=None,
68+
spi: SPI,
69+
chip_select: Optional[DigitalInOut] = None,
5970
*,
60-
cs_active_value=False,
61-
baudrate=100000,
62-
polarity=0,
63-
phase=0,
64-
extra_clocks=0
65-
):
71+
cs_active_value: bool = False,
72+
baudrate: int = 100000,
73+
polarity: int = 0,
74+
phase: int = 0,
75+
extra_clocks: int = 0
76+
) -> None:
6677
self.spi = spi
6778
self.baudrate = baudrate
6879
self.polarity = polarity
@@ -73,7 +84,7 @@ def __init__(
7384
if self.chip_select:
7485
self.chip_select.switch_to_output(value=True)
7586

76-
def __enter__(self):
87+
def __enter__(self) -> SPI:
7788
while not self.spi.try_lock():
7889
pass
7990
self.spi.configure(
@@ -83,7 +94,12 @@ def __enter__(self):
8394
self.chip_select.value = self.cs_active_value
8495
return self.spi
8596

86-
def __exit__(self, exc_type, exc_val, exc_tb):
97+
def __exit__(
98+
self,
99+
exc_type: Optional[Type[type]],
100+
exc_val: Optional[BaseException],
101+
exc_tb: Optional[TracebackType],
102+
) -> bool:
87103
if self.chip_select:
88104
self.chip_select.value = not self.cs_active_value
89105
if self.extra_clocks > 0:

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# Uncomment the below if you use native CircuitPython modules such as
2424
# digitalio, micropython and busio. List the modules you use. Without it, the
2525
# autodoc module docs will fail to generate with a warning.
26-
# autodoc_mock_imports = ["adafruit_bus_device", "micropython"]
26+
autodoc_mock_imports = ["busio", "digitalio", "circuitpython_typing", "_typing"]
2727

2828
intersphinx_mapping = {
2929
"python": ("https://docs.python.org/3", None),

0 commit comments

Comments
 (0)