Skip to content

Commit ca4f5e1

Browse files
authored
Merge pull request #21 from tekktrik/dev/add-typing
Add type annotations
2 parents aa525f7 + 57a5f06 commit ca4f5e1

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

adafruit_lis3mdl.py

Lines changed: 23 additions & 23 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,9 @@ class CV:
5965
"""struct helper"""
6066

6167
@classmethod
62-
def add_values(cls, value_tuples):
68+
def add_values(
69+
cls, value_tuples: Iterable[Tuple[str, int, Union[int, str], Optional[int]]]
70+
):
6371
"creates CV entires"
6472
cls.string = {}
6573
cls.lsb = {}
@@ -71,16 +79,14 @@ def add_values(cls, value_tuples):
7179
cls.lsb[value] = lsb
7280

7381
@classmethod
74-
def is_valid(cls, value):
82+
def is_valid(cls, value: int) -> bool:
7583
"Returns true if the given value is a member of the CV"
7684
return value in cls.string
7785

7886

7987
class Range(CV):
8088
"""Options for ``accelerometer_range``"""
8189

82-
pass # pylint: disable=unnecessary-pass
83-
8490

8591
Range.add_values(
8692
(
@@ -95,8 +101,6 @@ class Range(CV):
95101
class PerformanceMode(CV):
96102
"""Options for `performance_mode`"""
97103

98-
pass # pylint: disable=unnecessary-pass
99-
100104

101105
PerformanceMode.add_values(
102106
(
@@ -130,8 +134,6 @@ class Rate(CV):
130134
131135
"""
132136

133-
pass # pylint: disable=unnecessary-pass
134-
135137

136138
# The magnetometer data rate, includes FAST_ODR bit
137139
Rate.add_values(
@@ -164,8 +166,6 @@ class OperationMode(CV):
164166
============================= ============================================
165167
"""
166168

167-
pass # pylint: disable=unnecessary-pass
168-
169169

170170
OperationMode.add_values(
171171
(
@@ -186,7 +186,7 @@ class LIS3MDL:
186186
"""Driver for the LIS3MDL 3-axis magnetometer.
187187
188188
:param ~busio.I2C i2c_bus: The I2C bus the LIS3MDL is connected to.
189-
:param address: The I2C device address. Defaults to :const:`0x1C`
189+
:param int address: The I2C device address. Defaults to :const:`0x1C`
190190
191191
**Quickstart: Importing and using the device**
192192
@@ -228,7 +228,7 @@ class LIS3MDL:
228228
_range = RWBits(2, _LIS3MDL_CTRL_REG2, 5)
229229
_reset = RWBit(_LIS3MDL_CTRL_REG2, 2)
230230

231-
def __init__(self, i2c_bus, address=_LIS3MDL_DEFAULT_ADDRESS):
231+
def __init__(self, i2c_bus: I2C, address: int = _LIS3MDL_DEFAULT_ADDRESS) -> None:
232232
# pylint: disable=no-member
233233
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
234234
if self._chip_id != _LIS3MDL_CHIP_ID:
@@ -243,13 +243,13 @@ def __init__(self, i2c_bus, address=_LIS3MDL_DEFAULT_ADDRESS):
243243

244244
sleep(0.010)
245245

246-
def reset(self): # pylint: disable=no-self-use
246+
def reset(self) -> None:
247247
"""Reset the sensor to the default state set by the library"""
248248
self._reset = True
249249
sleep(0.010)
250250

251251
@property
252-
def magnetic(self):
252+
def magnetic(self) -> Tuple[float, float, float]:
253253
"""The processed magnetometer sensor values.
254254
A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
255255
"""
@@ -261,16 +261,16 @@ def magnetic(self):
261261

262262
return (x, y, z)
263263

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

267267
@property
268-
def range(self):
268+
def range(self) -> int:
269269
"""The measurement range for the magnetic sensor. Must be a ``Range``"""
270270
return self._range
271271

272272
@range.setter
273-
def range(self, value):
273+
def range(self, value: int) -> None:
274274
if not Range.is_valid(value):
275275
raise AttributeError("``range`` must be a ``Range``")
276276

@@ -279,12 +279,12 @@ def range(self, value):
279279
sleep(0.010)
280280

281281
@property
282-
def data_rate(self):
282+
def data_rate(self) -> int:
283283
"""The rate at which the sensor takes measurements. Must be a ``Rate``"""
284284
return self._data_rate
285285

286286
@data_rate.setter
287-
def data_rate(self, value):
287+
def data_rate(self, value: int) -> None:
288288
# pylint: disable=no-member
289289
if value is Rate.RATE_155_HZ:
290290
self.performance_mode = PerformanceMode.MODE_ULTRA
@@ -300,29 +300,29 @@ def data_rate(self, value):
300300
self._data_rate = value
301301

302302
@property
303-
def performance_mode(self):
303+
def performance_mode(self) -> int:
304304
"""Sets the 'performance mode' of the sensor. Must be a ``PerformanceMode``.
305305
Note that `performance_mode` affects the available data rate and will be
306306
automatically changed by setting ``data_rate`` to certain values."""
307307

308308
return self._perf_mode
309309

310310
@performance_mode.setter
311-
def performance_mode(self, value):
311+
def performance_mode(self, value: int) -> None:
312312
if not PerformanceMode.is_valid(value):
313313
raise AttributeError("`performance_mode` must be a `PerformanceMode`")
314314
self._perf_mode = value
315315
self._z_perf_mode = value
316316

317317
@property
318-
def operation_mode(self):
318+
def operation_mode(self) -> int:
319319
"""The operating mode for the sensor, controlling how measurements are taken.
320320
Must be an `OperationMode`. See the the `OperationMode` document for additional details
321321
"""
322322
return self._operation_mode
323323

324324
@operation_mode.setter
325-
def operation_mode(self, value):
325+
def operation_mode(self, value: int) -> None:
326326
if not OperationMode.is_valid(value):
327327
raise AttributeError("operation mode must be a OperationMode")
328328
self._operation_mode = value

0 commit comments

Comments
 (0)