Skip to content

Commit 7929b0b

Browse files
authored
Merge pull request #40 from tcfranks/main
Add Missing Type Annotations
2 parents 380f369 + 422e02c commit 7929b0b

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

adafruit_tca9548a.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
import time
3333
from micropython import const
3434

35+
try:
36+
from typing import List
37+
from typing_extensions import Literal
38+
from circuitpython_typing import WriteableBuffer, ReadableBuffer
39+
from busio import I2C
40+
except ImportError:
41+
pass
42+
3543
_DEFAULT_ADDRESS = const(0x70)
3644

3745
__version__ = "0.0.0+auto.0"
@@ -43,35 +51,41 @@ class TCA9548A_Channel:
4351
of the necessary I2C commands for channel switching. This class needs to
4452
behave like an I2CDevice."""
4553

46-
def __init__(self, tca, channel):
54+
def __init__(self, tca: "TCA9548A", channel: int) -> None:
4755
self.tca = tca
4856
self.channel_switch = bytearray([1 << channel])
4957

50-
def try_lock(self):
58+
def try_lock(self) -> bool:
5159
"""Pass through for try_lock."""
5260
while not self.tca.i2c.try_lock():
5361
time.sleep(0)
5462
self.tca.i2c.writeto(self.tca.address, self.channel_switch)
5563
return True
5664

57-
def unlock(self):
65+
def unlock(self) -> bool:
5866
"""Pass through for unlock."""
5967
self.tca.i2c.writeto(self.tca.address, b"\x00")
6068
return self.tca.i2c.unlock()
6169

62-
def readfrom_into(self, address, buffer, **kwargs):
70+
def readfrom_into(self, address: int, buffer: ReadableBuffer, **kwargs):
6371
"""Pass through for readfrom_into."""
6472
if address == self.tca.address:
6573
raise ValueError("Device address must be different than TCA9548A address.")
6674
return self.tca.i2c.readfrom_into(address, buffer, **kwargs)
6775

68-
def writeto(self, address, buffer, **kwargs):
76+
def writeto(self, address: int, buffer: WriteableBuffer, **kwargs):
6977
"""Pass through for writeto."""
7078
if address == self.tca.address:
7179
raise ValueError("Device address must be different than TCA9548A address.")
7280
return self.tca.i2c.writeto(address, buffer, **kwargs)
7381

74-
def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs):
82+
def writeto_then_readfrom(
83+
self,
84+
address: int,
85+
buffer_out: WriteableBuffer,
86+
buffer_in: ReadableBuffer,
87+
**kwargs
88+
):
7589
"""Pass through for writeto_then_readfrom."""
7690
# In linux, at least, this is a special kernel function call
7791
if address == self.tca.address:
@@ -80,23 +94,23 @@ def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs):
8094
address, buffer_out, buffer_in, **kwargs
8195
)
8296

83-
def scan(self):
97+
def scan(self) -> List[int]:
8498
"""Perform an I2C Device Scan"""
8599
return self.tca.i2c.scan()
86100

87101

88102
class TCA9548A:
89103
"""Class which provides interface to TCA9548A I2C multiplexer."""
90104

91-
def __init__(self, i2c, address=_DEFAULT_ADDRESS):
105+
def __init__(self, i2c: I2C, address: int = _DEFAULT_ADDRESS) -> None:
92106
self.i2c = i2c
93107
self.address = address
94108
self.channels = [None] * 8
95109

96-
def __len__(self):
110+
def __len__(self) -> Literal[8]:
97111
return 8
98112

99-
def __getitem__(self, key):
113+
def __getitem__(self, key: Literal[0, 1, 2, 3, 4, 5, 6, 7]) -> "TCA9548A_Channel":
100114
if not 0 <= key <= 7:
101115
raise IndexError("Channel must be an integer in the range: 0-7.")
102116
if self.channels[key] is None:

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-typing
8+
typing-extensions~=4.0

0 commit comments

Comments
 (0)