From 3277c45beda529f368289523cc0d82bd0f4e7b91 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Fri, 16 Sep 2022 09:18:13 -0400 Subject: [PATCH 1/4] Add Missing Type Annotations --- adafruit_lps2x.py | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index 184e53b..26b0113 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -40,6 +40,13 @@ from adafruit_register.i2c_bits import RWBits, ROBits from adafruit_register.i2c_bit import RWBit +try: + from typing import Optional, Tuple, Union + from typing_extensions import Literal + from busio import I2C +except ImportError: + pass + # _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address # _LPS2X_WHOAMI = 0x0F # Chip ID register # _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read @@ -74,7 +81,7 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples): + def add_values(cls, value_tuples: Tuple[str, int, float, Union[str, None]]) -> None: """creates CV entries""" cls.string = {} cls.lsb = {} @@ -86,7 +93,7 @@ def add_values(cls, value_tuples): cls.lsb[value] = lsb @classmethod - def is_valid(cls, value): + def is_valid(cls, value) -> bool: """Returns true if the given value is a member of the CV""" return value in cls.string @@ -139,23 +146,28 @@ class LPS2X: # pylint: disable=too-many-instance-attributes _raw_temperature = ROUnaryStruct(_LPS2X_TEMP_OUT_L, " None: self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) if not self._chip_id in [chip_id]: raise RuntimeError( - "Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id + f"Failed to find LPS2X! Found chip ID {hex(self._chip_id)}" ) self.reset() self.initialize() sleep(0.010) # delay 10ms for first reading - def initialize(self): # pylint: disable=no-self-use + def initialize(self) -> None: # pylint: disable=no-self-use """Configure the sensor with the default settings. For use after calling :meth:`reset`""" raise RuntimeError( "LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead" ) # override in subclass - def reset(self): + def reset(self) -> None: """Reset the sensor, restoring all configuration registers to their defaults""" self._reset = True # wait for the reset to finish @@ -163,7 +175,7 @@ def reset(self): pass @property - def pressure(self): + def pressure(self) -> float: """The current pressure measurement in hPa""" raw = self._raw_pressure @@ -172,7 +184,7 @@ def pressure(self): return raw / 4096.0 @property - def temperature(self): + def temperature(self) -> float: """The current temperature measurement in degrees Celsius""" raw_temperature = self._raw_temperature @@ -181,14 +193,14 @@ def temperature(self): ) + self._temp_offset # pylint:disable=no-member @property - def data_rate(self): + def data_rate(self) -> int: """The rate at which the sensor measures :attr:`pressure` and :attr:`temperature`. :attr:`data_rate` should be set to one of the values of :class:`adafruit_lps2x.Rate`""" return self._data_rate @data_rate.setter - def data_rate(self, value): + def data_rate(self, value: int) -> None: if not Rate.is_valid(value): raise AttributeError("data_rate must be a `Rate`") @@ -209,7 +221,9 @@ class LPS25(LPS2X): _reset = RWBit(_LPS25_CTRL_REG2, 2) _data_rate = RWBits(3, _LPS25_CTRL_REG1, 4) - def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS): + def __init__( + self, i2c_bus: I2C, address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS + ) -> None: Rate.add_values( ( @@ -226,7 +240,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS): self._temp_offset = 42.5 # self._inc_spi_flag = 0x40 - def initialize(self): + def initialize(self) -> None: """Configure the sensor with the default settings. For use after calling :func:`LPS2X.reset` """ @@ -249,7 +263,9 @@ class LPS22(LPS2X): _reset = RWBit(_LPS22_CTRL_REG2, 2) _data_rate = RWBits(3, _LPS22_CTRL_REG1, 4) - def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS): + def __init__( + self, i2c_bus: I2C, address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS + ) -> None: # Only adding Class-appropriate rates Rate.add_values( ( @@ -266,7 +282,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS): self._temp_scaling = 100 self._temp_offset = 0 - def initialize(self): + def initialize(self) -> None: """Configure the sensor with the default settings. For use after calling :func:`LPS2X.reset` """ From c29c339a82b06d93f17f4aefa887c62d79893355 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sun, 25 Sep 2022 20:14:01 -0400 Subject: [PATCH 2/4] added Iterable and other changes per review comments --- adafruit_lps2x.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index 26b0113..cb0feaf 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -41,7 +41,7 @@ from adafruit_register.i2c_bit import RWBit try: - from typing import Optional, Tuple, Union + from typing import Iterable, Optional, Tuple from typing_extensions import Literal from busio import I2C except ImportError: @@ -81,7 +81,9 @@ class CV: """struct helper""" @classmethod - def add_values(cls, value_tuples: Tuple[str, int, float, Union[str, None]]) -> None: + def add_values( + cls, value_tuples: Iterable[Tuple[str, int, Optional[float], Optional[float]]] + ) -> None: """creates CV entries""" cls.string = {} cls.lsb = {} @@ -149,8 +151,8 @@ class LPS2X: # pylint: disable=too-many-instance-attributes def __init__( self, i2c_bus: I2C, - address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS, - chip_id: Optional[Literal[0xB1, 0xBD]] = None, + address: int = _LPS2X_DEFAULT_ADDRESS, + chip_id: Optional[int] = None, ) -> None: self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) if not self._chip_id in [chip_id]: From 4b2ec3f0ee43ea260f0f7f2b3060a5bbca8366b8 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sun, 25 Sep 2022 20:17:14 -0400 Subject: [PATCH 3/4] added Iterable and other changes per review comments --- adafruit_lps2x.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index cb0feaf..e6485e6 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -95,7 +95,7 @@ def add_values( cls.lsb[value] = lsb @classmethod - def is_valid(cls, value) -> bool: + def is_valid(cls, value: int) -> bool: """Returns true if the given value is a member of the CV""" return value in cls.string From 1a86e3ce5d24730f5daf336ded1750a20f1aa411 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Tue, 27 Sep 2022 21:53:07 -0400 Subject: [PATCH 4/4] added Iterable and other changes per review comments --- adafruit_lps2x.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index e6485e6..d63179d 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -149,11 +149,10 @@ class LPS2X: # pylint: disable=too-many-instance-attributes _raw_pressure = ROBits(24, _LPS2X_PRESS_OUT_XL, 0, 3) def __init__( - self, - i2c_bus: I2C, - address: int = _LPS2X_DEFAULT_ADDRESS, - chip_id: Optional[int] = None, + self, i2c_bus: I2C, address: int = _LPS2X_DEFAULT_ADDRESS, chip_id: int = -1 ) -> None: + if chip_id == -1: + raise ValueError("Must set the chip_id argument") self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) if not self._chip_id in [chip_id]: raise RuntimeError( @@ -223,9 +222,7 @@ class LPS25(LPS2X): _reset = RWBit(_LPS25_CTRL_REG2, 2) _data_rate = RWBits(3, _LPS25_CTRL_REG1, 4) - def __init__( - self, i2c_bus: I2C, address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS - ) -> None: + def __init__(self, i2c_bus: I2C, address: int = _LPS2X_DEFAULT_ADDRESS) -> None: Rate.add_values( (