Skip to content

Commit 8f6c517

Browse files
committed
Add type annotations, remove unnecessary pylint disables
1 parent 5df02b1 commit 8f6c517

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

adafruit_lis3mdl.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_register.i2c_bits import RWBits
3434
from adafruit_register.i2c_bit import RWBit
3535

36+
try:
37+
from typing import Iterable, Tuple, Union, Optional
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0-auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LIS3MDL.git"
3844

@@ -59,7 +65,7 @@ class CV:
5965
"""struct helper"""
6066

6167
@classmethod
62-
def add_values(cls, value_tuples):
68+
def add_values(cls, value_tuples: Iterable[Tuple[str, int, Union[int, str], Optional[int]]]):
6369
"creates CV entires"
6470
cls.string = {}
6571
cls.lsb = {}
@@ -71,7 +77,7 @@ def add_values(cls, value_tuples):
7177
cls.lsb[value] = lsb
7278

7379
@classmethod
74-
def is_valid(cls, value):
80+
def is_valid(cls, value: int) -> bool:
7581
"Returns true if the given value is a member of the CV"
7682
return value in cls.string
7783

@@ -178,7 +184,7 @@ class LIS3MDL:
178184
"""Driver for the LIS3MDL 3-axis magnetometer.
179185
180186
:param ~busio.I2C i2c_bus: The I2C bus the LIS3MDL is connected to.
181-
:param address: The I2C device address. Defaults to :const:`0x1C`
187+
:param int address: The I2C device address. Defaults to :const:`0x1C`
182188
183189
**Quickstart: Importing and using the device**
184190
@@ -220,7 +226,7 @@ class LIS3MDL:
220226
_range = RWBits(2, _LIS3MDL_CTRL_REG2, 5)
221227
_reset = RWBit(_LIS3MDL_CTRL_REG2, 2)
222228

223-
def __init__(self, i2c_bus, address=_LIS3MDL_DEFAULT_ADDRESS):
229+
def __init__(self, i2c_bus: I2C, address: int = _LIS3MDL_DEFAULT_ADDRESS) -> None:
224230
# pylint: disable=no-member
225231
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
226232
if self._chip_id != _LIS3MDL_CHIP_ID:
@@ -235,13 +241,13 @@ def __init__(self, i2c_bus, address=_LIS3MDL_DEFAULT_ADDRESS):
235241

236242
sleep(0.010)
237243

238-
def reset(self): # pylint: disable=no-self-use
244+
def reset(self) -> None:
239245
"""Reset the sensor to the default state set by the library"""
240246
self._reset = True
241247
sleep(0.010)
242248

243249
@property
244-
def magnetic(self):
250+
def magnetic(self) -> Tuple[float, float, float]:
245251
"""The processed magnetometer sensor values.
246252
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
247253
"""
@@ -253,16 +259,16 @@ def magnetic(self):
253259

254260
return (x, y, z)
255261

256-
def _scale_mag_data(self, raw_measurement): # pylint: disable=no-self-use
262+
def _scale_mag_data(self, raw_measurement: int) -> float:
257263
return (raw_measurement / Range.lsb[self.range]) * _GAUSS_TO_UT
258264

259265
@property
260-
def range(self):
266+
def range(self) -> int:
261267
"""The measurement range for the magnetic sensor. Must be a ``Range``"""
262268
return self._range
263269

264270
@range.setter
265-
def range(self, value):
271+
def range(self, value: int) -> None:
266272
if not Range.is_valid(value):
267273
raise AttributeError("``range`` must be a ``Range``")
268274

@@ -271,12 +277,12 @@ def range(self, value):
271277
sleep(0.010)
272278

273279
@property
274-
def data_rate(self):
280+
def data_rate(self) -> int:
275281
"""The rate at which the sensor takes measurements. Must be a ``Rate``"""
276282
return self._data_rate
277283

278284
@data_rate.setter
279-
def data_rate(self, value):
285+
def data_rate(self, value: int) -> None:
280286
# pylint: disable=no-member
281287
if value is Rate.RATE_155_HZ:
282288
self.performance_mode = PerformanceMode.MODE_ULTRA
@@ -292,29 +298,29 @@ def data_rate(self, value):
292298
self._data_rate = value
293299

294300
@property
295-
def performance_mode(self):
301+
def performance_mode(self) -> int:
296302
"""Sets the 'performance mode' of the sensor. Must be a ``PerformanceMode``.
297303
Note that `performance_mode` affects the available data rate and will be
298304
automatically changed by setting ``data_rate`` to certain values."""
299305

300306
return self._perf_mode
301307

302308
@performance_mode.setter
303-
def performance_mode(self, value):
309+
def performance_mode(self, value: int) -> None:
304310
if not PerformanceMode.is_valid(value):
305311
raise AttributeError("`performance_mode` must be a `PerformanceMode`")
306312
self._perf_mode = value
307313
self._z_perf_mode = value
308314

309315
@property
310-
def operation_mode(self):
316+
def operation_mode(self) -> int:
311317
"""The operating mode for the sensor, controlling how measurements are taken.
312318
Must be an `OperationMode`. See the the `OperationMode` document for additional details
313319
"""
314320
return self._operation_mode
315321

316322
@operation_mode.setter
317-
def operation_mode(self, value):
323+
def operation_mode(self, value: int) -> None:
318324
if not OperationMode.is_valid(value):
319325
raise AttributeError("operation mode must be a OperationMode")
320326
self._operation_mode = value

0 commit comments

Comments
 (0)