Skip to content

Commit 3277c45

Browse files
committed
Add Missing Type Annotations
1 parent e154a64 commit 3277c45

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

adafruit_lps2x.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
from adafruit_register.i2c_bits import RWBits, ROBits
4141
from adafruit_register.i2c_bit import RWBit
4242

43+
try:
44+
from typing import Optional, Tuple, Union
45+
from typing_extensions import Literal
46+
from busio import I2C
47+
except ImportError:
48+
pass
49+
4350
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
4451
# _LPS2X_WHOAMI = 0x0F # Chip ID register
4552
# _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read
@@ -74,7 +81,7 @@ class CV:
7481
"""struct helper"""
7582

7683
@classmethod
77-
def add_values(cls, value_tuples):
84+
def add_values(cls, value_tuples: Tuple[str, int, float, Union[str, None]]) -> None:
7885
"""creates CV entries"""
7986
cls.string = {}
8087
cls.lsb = {}
@@ -86,7 +93,7 @@ def add_values(cls, value_tuples):
8693
cls.lsb[value] = lsb
8794

8895
@classmethod
89-
def is_valid(cls, value):
96+
def is_valid(cls, value) -> bool:
9097
"""Returns true if the given value is a member of the CV"""
9198
return value in cls.string
9299

@@ -139,31 +146,36 @@ class LPS2X: # pylint: disable=too-many-instance-attributes
139146
_raw_temperature = ROUnaryStruct(_LPS2X_TEMP_OUT_L, "<h")
140147
_raw_pressure = ROBits(24, _LPS2X_PRESS_OUT_XL, 0, 3)
141148

142-
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
149+
def __init__(
150+
self,
151+
i2c_bus: I2C,
152+
address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS,
153+
chip_id: Optional[Literal[0xB1, 0xBD]] = None,
154+
) -> None:
143155
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
144156
if not self._chip_id in [chip_id]:
145157
raise RuntimeError(
146-
"Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id
158+
f"Failed to find LPS2X! Found chip ID {hex(self._chip_id)}"
147159
)
148160
self.reset()
149161
self.initialize()
150162
sleep(0.010) # delay 10ms for first reading
151163

152-
def initialize(self): # pylint: disable=no-self-use
164+
def initialize(self) -> None: # pylint: disable=no-self-use
153165
"""Configure the sensor with the default settings. For use after calling :meth:`reset`"""
154166
raise RuntimeError(
155167
"LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead"
156168
) # override in subclass
157169

158-
def reset(self):
170+
def reset(self) -> None:
159171
"""Reset the sensor, restoring all configuration registers to their defaults"""
160172
self._reset = True
161173
# wait for the reset to finish
162174
while self._reset:
163175
pass
164176

165177
@property
166-
def pressure(self):
178+
def pressure(self) -> float:
167179
"""The current pressure measurement in hPa"""
168180
raw = self._raw_pressure
169181

@@ -172,7 +184,7 @@ def pressure(self):
172184
return raw / 4096.0
173185

174186
@property
175-
def temperature(self):
187+
def temperature(self) -> float:
176188
"""The current temperature measurement in degrees Celsius"""
177189

178190
raw_temperature = self._raw_temperature
@@ -181,14 +193,14 @@ def temperature(self):
181193
) + self._temp_offset # pylint:disable=no-member
182194

183195
@property
184-
def data_rate(self):
196+
def data_rate(self) -> int:
185197
"""The rate at which the sensor measures :attr:`pressure` and
186198
:attr:`temperature`. :attr:`data_rate` should be set to one of
187199
the values of :class:`adafruit_lps2x.Rate`"""
188200
return self._data_rate
189201

190202
@data_rate.setter
191-
def data_rate(self, value):
203+
def data_rate(self, value: int) -> None:
192204
if not Rate.is_valid(value):
193205
raise AttributeError("data_rate must be a `Rate`")
194206

@@ -209,7 +221,9 @@ class LPS25(LPS2X):
209221
_reset = RWBit(_LPS25_CTRL_REG2, 2)
210222
_data_rate = RWBits(3, _LPS25_CTRL_REG1, 4)
211223

212-
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
224+
def __init__(
225+
self, i2c_bus: I2C, address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS
226+
) -> None:
213227

214228
Rate.add_values(
215229
(
@@ -226,7 +240,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
226240
self._temp_offset = 42.5
227241
# self._inc_spi_flag = 0x40
228242

229-
def initialize(self):
243+
def initialize(self) -> None:
230244
"""Configure the sensor with the default settings.
231245
For use after calling :func:`LPS2X.reset`
232246
"""
@@ -249,7 +263,9 @@ class LPS22(LPS2X):
249263
_reset = RWBit(_LPS22_CTRL_REG2, 2)
250264
_data_rate = RWBits(3, _LPS22_CTRL_REG1, 4)
251265

252-
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
266+
def __init__(
267+
self, i2c_bus: I2C, address: Literal[0x5C, 0x5D] = _LPS2X_DEFAULT_ADDRESS
268+
) -> None:
253269
# Only adding Class-appropriate rates
254270
Rate.add_values(
255271
(
@@ -266,7 +282,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
266282
self._temp_scaling = 100
267283
self._temp_offset = 0
268284

269-
def initialize(self):
285+
def initialize(self) -> None:
270286
"""Configure the sensor with the default settings.
271287
For use after calling :func:`LPS2X.reset`
272288
"""

0 commit comments

Comments
 (0)