Skip to content

Commit 4d70e9f

Browse files
authored
Merge pull request #32 from tcfranks/main
Correct Missing Type Annotations
2 parents 1e8a9ba + 11c59a0 commit 4d70e9f

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

adafruit_sht31d.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
from micropython import const
3434
from adafruit_bus_device.i2c_device import I2CDevice
3535

36+
try:
37+
from typing import List, Tuple, Union
38+
from typing_extensions import Literal
39+
from circuitpython_typing import ReadableBuffer
40+
from busio import I2C
41+
except ImportError:
42+
pass
43+
44+
3645
__version__ = "0.0.0+auto.0"
3746
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHT31D.git"
3847

@@ -108,7 +117,7 @@
108117
_DELAY = ((REP_LOW, 0.0045), (REP_MED, 0.0065), (REP_HIGH, 0.0155))
109118

110119

111-
def _crc(data):
120+
def _crc(data) -> int:
112121
crc = 0xFF
113122
for byte in data:
114123
crc ^= byte
@@ -121,7 +130,7 @@ def _crc(data):
121130
return crc & 0xFF
122131

123132

124-
def _unpack(data):
133+
def _unpack(data: ReadableBuffer) -> List[int]:
125134
length = len(data)
126135
crc = [None] * (length // 3)
127136
word = [None] * (length // 3)
@@ -172,9 +181,9 @@ class SHT31D:
172181
173182
"""
174183

175-
def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
184+
def __init__(self, i2c_bus: I2C, address: int = _SHT31_DEFAULT_ADDRESS) -> None:
176185
if address not in _SHT31_ADDRESSES:
177-
raise ValueError("Invalid address: 0x%x" % (address))
186+
raise ValueError(f"Invalid address: {hex(address)}")
178187
self.i2c_device = I2CDevice(i2c_bus, address)
179188
self._mode = MODE_SINGLE
180189
self._repeatability = REP_HIGH
@@ -186,11 +195,11 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS):
186195
self._cached_humidity = None
187196
self._reset()
188197

189-
def _command(self, command):
198+
def _command(self, command: int) -> None:
190199
with self.i2c_device as i2c:
191200
i2c.write(struct.pack(">H", command))
192201

193-
def _reset(self):
202+
def _reset(self) -> None:
194203
"""
195204
Soft reset the device
196205
The reset command is preceded by a break command as the
@@ -201,7 +210,7 @@ def _reset(self):
201210
self._command(_SHT31_SOFTRESET)
202211
time.sleep(0.0015)
203212

204-
def _periodic(self):
213+
def _periodic(self) -> None:
205214
for command in _PERIODIC_COMMANDS:
206215
if self.art == command[0] or (
207216
self.repeatability == command[0] and self.frequency == command[1]
@@ -210,7 +219,7 @@ def _periodic(self):
210219
time.sleep(0.001)
211220
self._last_read = 0
212221

213-
def _data(self):
222+
def _data(self) -> Union[Tuple[float, float], Tuple[List[float], List[float]]]:
214223
if self.mode == MODE_PERIODIC:
215224
data = bytearray(48)
216225
data[0] = 0xFF
@@ -244,7 +253,7 @@ def _data(self):
244253
return temperature[0], humidity[0]
245254
return temperature, humidity
246255

247-
def _read(self):
256+
def _read(self) -> Union[Tuple[float, float], Tuple[List[float], List[float]]]:
248257
if (
249258
self.mode == MODE_PERIODIC
250259
and time.time() > self._last_read + 1 / self.frequency
@@ -256,7 +265,7 @@ def _read(self):
256265
return self._cached_temperature, self._cached_humidity
257266

258267
@property
259-
def mode(self):
268+
def mode(self) -> Literal["Single", "Periodic"]:
260269
"""
261270
Operation mode
262271
Allowed values are the constants MODE_*
@@ -265,9 +274,9 @@ def mode(self):
265274
return self._mode
266275

267276
@mode.setter
268-
def mode(self, value):
277+
def mode(self, value: Literal["Single", "Periodic"]) -> None:
269278
if not value in _SHT31_MODES:
270-
raise ValueError("Mode '%s' not supported" % (value))
279+
raise ValueError(f"Mode '{value}' not supported")
271280
if self._mode == MODE_PERIODIC and value != MODE_PERIODIC:
272281
self._command(_SHT31_PERIODIC_BREAK)
273282
time.sleep(0.001)
@@ -276,45 +285,45 @@ def mode(self, value):
276285
self._mode = value
277286

278287
@property
279-
def repeatability(self):
288+
def repeatability(self) -> Literal["High", "Medium", "Low"]:
280289
"""
281290
Repeatability
282291
Allowed values are the constants REP_*
283292
"""
284293
return self._repeatability
285294

286295
@repeatability.setter
287-
def repeatability(self, value):
296+
def repeatability(self, value: Literal["High", "Medium", "Low"]) -> None:
288297
if not value in _SHT31_REP:
289-
raise ValueError("Repeatability '%s' not supported" % (value))
298+
raise ValueError("Repeatability '{value}' not supported")
290299
if self.mode == MODE_PERIODIC and not self._repeatability == value:
291300
self._repeatability = value
292301
self._periodic()
293302
else:
294303
self._repeatability = value
295304

296305
@property
297-
def clock_stretching(self):
306+
def clock_stretching(self) -> bool:
298307
"""
299308
Control clock stretching.
300309
This feature only affects 'Single' mode.
301310
"""
302311
return self._clock_stretching
303312

304313
@clock_stretching.setter
305-
def clock_stretching(self, value):
314+
def clock_stretching(self, value: bool) -> None:
306315
self._clock_stretching = bool(value)
307316

308317
@property
309-
def art(self):
318+
def art(self) -> bool:
310319
"""
311320
Control accelerated response time
312321
This feature only affects 'Periodic' mode.
313322
"""
314323
return self._art
315324

316325
@art.setter
317-
def art(self, value):
326+
def art(self, value: bool) -> None:
318327
if value:
319328
self.frequency = FREQUENCY_4
320329
if self.mode == MODE_PERIODIC and not self._art == value:
@@ -324,7 +333,7 @@ def art(self, value):
324333
self._art = bool(value)
325334

326335
@property
327-
def frequency(self):
336+
def frequency(self) -> float:
328337
"""
329338
Periodic data acquisition frequency
330339
Allowed values are the constants FREQUENCY_*
@@ -333,21 +342,19 @@ def frequency(self):
333342
return self._frequency
334343

335344
@frequency.setter
336-
def frequency(self, value):
345+
def frequency(self, value: float) -> None:
337346
if self.art:
338347
raise RuntimeError("Frequency locked to '4 Hz' when ART enabled")
339348
if not value in _SHT31_FREQUENCIES:
340-
raise ValueError(
341-
"Data acquisition frequency '%s Hz' not supported" % (value)
342-
)
349+
raise ValueError("Data acquisition frequency '{value} Hz' not supported")
343350
if self.mode == MODE_PERIODIC and not self._frequency == value:
344351
self._frequency = value
345352
self._periodic()
346353
else:
347354
self._frequency = value
348355

349356
@property
350-
def temperature(self):
357+
def temperature(self) -> Union[float, List[float]]:
351358
"""
352359
The measured temperature in degrees Celsius.
353360
'Single' mode reads and returns the current temperature as a float.
@@ -360,7 +367,7 @@ def temperature(self):
360367
return temperature
361368

362369
@property
363-
def relative_humidity(self):
370+
def relative_humidity(self) -> Union[float, List[float]]:
364371
"""
365372
The measured relative humidity in percent.
366373
'Single' mode reads and returns the current humidity as a float.
@@ -373,12 +380,12 @@ def relative_humidity(self):
373380
return humidity
374381

375382
@property
376-
def heater(self):
383+
def heater(self) -> bool:
377384
"""Control device's internal heater."""
378385
return (self.status & 0x2000) != 0
379386

380387
@heater.setter
381-
def heater(self, value=False):
388+
def heater(self, value: bool = False) -> None:
382389
if value:
383390
self._command(_SHT31_HEATER_ENABLE)
384391
time.sleep(0.001)
@@ -387,7 +394,7 @@ def heater(self, value=False):
387394
time.sleep(0.001)
388395

389396
@property
390-
def status(self):
397+
def status(self) -> int:
391398
"""Device status."""
392399
data = bytearray(2)
393400
self._command(_SHT31_READSTATUS)
@@ -398,7 +405,7 @@ def status(self):
398405
return status
399406

400407
@property
401-
def serial_number(self):
408+
def serial_number(self) -> int:
402409
"""Device serial number."""
403410
data = bytearray(6)
404411
data[0] = 0xFF

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
typing-extensions~=4.0
8+
adafruit-circuitpython-typing

0 commit comments

Comments
 (0)