From 88509798d7a96336b533e73029cc61fd7c81bc66 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sun, 11 Sep 2022 19:52:44 -0400 Subject: [PATCH 1/2] Add Missing Type Annotations --- adafruit_tca9548a.py | 34 ++++++++++++++++++++++++---------- requirements.txt | 2 ++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/adafruit_tca9548a.py b/adafruit_tca9548a.py index a955f39..bbf976c 100644 --- a/adafruit_tca9548a.py +++ b/adafruit_tca9548a.py @@ -32,6 +32,14 @@ import time from micropython import const +try: + from typing import Optional, List # pylint: disable=unused-import + from typing_extensions import Literal + from circuitpython_typing import WriteableBuffer, ReadableBuffer + from busio import I2C +except ImportError: + pass + _DEFAULT_ADDRESS = const(0x70) __version__ = "0.0.0+auto.0" @@ -43,35 +51,41 @@ class TCA9548A_Channel: of the necessary I2C commands for channel switching. This class needs to behave like an I2CDevice.""" - def __init__(self, tca, channel): + def __init__(self, tca: "TCA9548A", channel: int) -> None: self.tca = tca self.channel_switch = bytearray([1 << channel]) - def try_lock(self): + def try_lock(self) -> bool: """Pass through for try_lock.""" while not self.tca.i2c.try_lock(): time.sleep(0) self.tca.i2c.writeto(self.tca.address, self.channel_switch) return True - def unlock(self): + def unlock(self) -> bool: """Pass through for unlock.""" self.tca.i2c.writeto(self.tca.address, b"\x00") return self.tca.i2c.unlock() - def readfrom_into(self, address, buffer, **kwargs): + def readfrom_into(self, address: int, buffer: ReadableBuffer, **kwargs): """Pass through for readfrom_into.""" if address == self.tca.address: raise ValueError("Device address must be different than TCA9548A address.") return self.tca.i2c.readfrom_into(address, buffer, **kwargs) - def writeto(self, address, buffer, **kwargs): + def writeto(self, address: int, buffer: WriteableBuffer, **kwargs): """Pass through for writeto.""" if address == self.tca.address: raise ValueError("Device address must be different than TCA9548A address.") return self.tca.i2c.writeto(address, buffer, **kwargs) - def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs): + def writeto_then_readfrom( + self, + address: int, + buffer_out: WriteableBuffer, + buffer_in: ReadableBuffer, + **kwargs + ): """Pass through for writeto_then_readfrom.""" # In linux, at least, this is a special kernel function call if address == self.tca.address: @@ -80,7 +94,7 @@ def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs): address, buffer_out, buffer_in, **kwargs ) - def scan(self): + def scan(self) -> List[int]: """Perform an I2C Device Scan""" return self.tca.i2c.scan() @@ -88,15 +102,15 @@ def scan(self): class TCA9548A: """Class which provides interface to TCA9548A I2C multiplexer.""" - def __init__(self, i2c, address=_DEFAULT_ADDRESS): + def __init__(self, i2c: I2C, address: int = _DEFAULT_ADDRESS) -> None: self.i2c = i2c self.address = address self.channels = [None] * 8 - def __len__(self): + def __len__(self) -> Literal[8]: return 8 - def __getitem__(self, key): + def __getitem__(self, key: Literal[0, 1, 2, 3, 4, 5, 6, 7]) -> "TCA9548A_Channel": if not 0 <= key <= 7: raise IndexError("Channel must be an integer in the range: 0-7.") if self.channels[key] is None: diff --git a/requirements.txt b/requirements.txt index a45c547..b7b467e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ Adafruit-Blinka adafruit-circuitpython-busdevice +adafruit-circuitpython-typing +typing-extensions~=4.0 From 422e02c7cbe48c47c41368de693e6d00444e092c Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 12 Sep 2022 11:51:41 -0400 Subject: [PATCH 2/2] Add Missing Type Annotations --- adafruit_tca9548a.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_tca9548a.py b/adafruit_tca9548a.py index bbf976c..24fddd7 100644 --- a/adafruit_tca9548a.py +++ b/adafruit_tca9548a.py @@ -33,7 +33,7 @@ from micropython import const try: - from typing import Optional, List # pylint: disable=unused-import + from typing import List from typing_extensions import Literal from circuitpython_typing import WriteableBuffer, ReadableBuffer from busio import I2C