From a0753ab0f0bb4eae9715d051b6950305c24d6c22 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sat, 20 Aug 2022 14:59:02 -0400 Subject: [PATCH 1/6] Correct Missing Type Annotations --- adafruit_sht31d.py | 63 +++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/adafruit_sht31d.py b/adafruit_sht31d.py index b36a728..224fb1a 100644 --- a/adafruit_sht31d.py +++ b/adafruit_sht31d.py @@ -33,6 +33,13 @@ from micropython import const from adafruit_bus_device.i2c_device import I2CDevice +try: + from typing import List, Tuple, Union + from busio import I2C +except ImportError: + pass + + __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SHT31D.git" @@ -108,7 +115,7 @@ _DELAY = ((REP_LOW, 0.0045), (REP_MED, 0.0065), (REP_HIGH, 0.0155)) -def _crc(data): +def _crc(data) -> int: crc = 0xFF for byte in data: crc ^= byte @@ -121,7 +128,7 @@ def _crc(data): return crc & 0xFF -def _unpack(data): +def _unpack(data) -> float: length = len(data) crc = [None] * (length // 3) word = [None] * (length // 3) @@ -172,9 +179,9 @@ class SHT31D: """ - def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS): + def __init__(self, i2c_bus: I2C, address: int = _SHT31_DEFAULT_ADDRESS) -> None: if address not in _SHT31_ADDRESSES: - raise ValueError("Invalid address: 0x%x" % (address)) + raise ValueError(f"Invalid address: {hex(address)}") self.i2c_device = I2CDevice(i2c_bus, address) self._mode = MODE_SINGLE self._repeatability = REP_HIGH @@ -186,11 +193,11 @@ def __init__(self, i2c_bus, address=_SHT31_DEFAULT_ADDRESS): self._cached_humidity = None self._reset() - def _command(self, command): + def _command(self, command: int) -> None: with self.i2c_device as i2c: i2c.write(struct.pack(">H", command)) - def _reset(self): + def _reset(self) -> None: """ Soft reset the device The reset command is preceded by a break command as the @@ -201,7 +208,7 @@ def _reset(self): self._command(_SHT31_SOFTRESET) time.sleep(0.0015) - def _periodic(self): + def _periodic(self) -> None: for command in _PERIODIC_COMMANDS: if self.art == command[0] or ( self.repeatability == command[0] and self.frequency == command[1] @@ -210,7 +217,7 @@ def _periodic(self): time.sleep(0.001) self._last_read = 0 - def _data(self): + def _data(self) -> Tuple[float, float]: if self.mode == MODE_PERIODIC: data = bytearray(48) data[0] = 0xFF @@ -244,7 +251,7 @@ def _data(self): return temperature[0], humidity[0] return temperature, humidity - def _read(self): + def _read(self) -> Tuple[float, float]: if ( self.mode == MODE_PERIODIC and time.time() > self._last_read + 1 / self.frequency @@ -256,7 +263,7 @@ def _read(self): return self._cached_temperature, self._cached_humidity @property - def mode(self): + def mode(self) -> str: """ Operation mode Allowed values are the constants MODE_* @@ -265,9 +272,9 @@ def mode(self): return self._mode @mode.setter - def mode(self, value): + def mode(self, value: str): if not value in _SHT31_MODES: - raise ValueError("Mode '%s' not supported" % (value)) + raise ValueError(f"Mode '{value}' not supported") if self._mode == MODE_PERIODIC and value != MODE_PERIODIC: self._command(_SHT31_PERIODIC_BREAK) time.sleep(0.001) @@ -276,7 +283,7 @@ def mode(self, value): self._mode = value @property - def repeatability(self): + def repeatability(self) -> Tuple[str, bool, int]: """ Repeatability Allowed values are the constants REP_* @@ -284,9 +291,9 @@ def repeatability(self): return self._repeatability @repeatability.setter - def repeatability(self, value): + def repeatability(self, value: Tuple[str, bool, int]) -> None: if not value in _SHT31_REP: - raise ValueError("Repeatability '%s' not supported" % (value)) + raise ValueError("Repeatability '{value}' not supported") if self.mode == MODE_PERIODIC and not self._repeatability == value: self._repeatability = value self._periodic() @@ -294,7 +301,7 @@ def repeatability(self, value): self._repeatability = value @property - def clock_stretching(self): + def clock_stretching(self) -> bool: """ Control clock stretching. This feature only affects 'Single' mode. @@ -302,11 +309,11 @@ def clock_stretching(self): return self._clock_stretching @clock_stretching.setter - def clock_stretching(self, value): + def clock_stretching(self, value: bool) -> None: self._clock_stretching = bool(value) @property - def art(self): + def art(self) -> bool: """ Control accelerated response time This feature only affects 'Periodic' mode. @@ -314,7 +321,7 @@ def art(self): return self._art @art.setter - def art(self, value): + def art(self, value: bool) -> None: if value: self.frequency = FREQUENCY_4 if self.mode == MODE_PERIODIC and not self._art == value: @@ -324,7 +331,7 @@ def art(self, value): self._art = bool(value) @property - def frequency(self): + def frequency(self) -> float: """ Periodic data acquisition frequency Allowed values are the constants FREQUENCY_* @@ -333,12 +340,12 @@ def frequency(self): return self._frequency @frequency.setter - def frequency(self, value): + def frequency(self, value: float) -> None: if self.art: raise RuntimeError("Frequency locked to '4 Hz' when ART enabled") if not value in _SHT31_FREQUENCIES: raise ValueError( - "Data acquisition frequency '%s Hz' not supported" % (value) + "Data acquisition frequency '{value} Hz' not supported" ) if self.mode == MODE_PERIODIC and not self._frequency == value: self._frequency = value @@ -347,7 +354,7 @@ def frequency(self, value): self._frequency = value @property - def temperature(self): + def temperature(self) -> Union(float, List[float]): """ The measured temperature in degrees Celsius. 'Single' mode reads and returns the current temperature as a float. @@ -360,7 +367,7 @@ def temperature(self): return temperature @property - def relative_humidity(self): + def relative_humidity(self) -> Union(float, List[float]): """ The measured relative humidity in percent. 'Single' mode reads and returns the current humidity as a float. @@ -373,12 +380,12 @@ def relative_humidity(self): return humidity @property - def heater(self): + def heater(self) -> bool: """Control device's internal heater.""" return (self.status & 0x2000) != 0 @heater.setter - def heater(self, value=False): + def heater(self, value: bool = False) -> None: if value: self._command(_SHT31_HEATER_ENABLE) time.sleep(0.001) @@ -387,7 +394,7 @@ def heater(self, value=False): time.sleep(0.001) @property - def status(self): + def status(self) -> int: """Device status.""" data = bytearray(2) self._command(_SHT31_READSTATUS) @@ -398,7 +405,7 @@ def status(self): return status @property - def serial_number(self): + def serial_number(self) -> int: """Device serial number.""" data = bytearray(6) data[0] = 0xFF From 353a1d22a1346b6835d630403bbd188bd3135061 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sat, 20 Aug 2022 14:59:45 -0400 Subject: [PATCH 2/6] Correct Missing Type Annotations --- adafruit_sht31d.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_sht31d.py b/adafruit_sht31d.py index 224fb1a..6fa737f 100644 --- a/adafruit_sht31d.py +++ b/adafruit_sht31d.py @@ -344,9 +344,7 @@ def frequency(self, value: float) -> None: if self.art: raise RuntimeError("Frequency locked to '4 Hz' when ART enabled") if not value in _SHT31_FREQUENCIES: - raise ValueError( - "Data acquisition frequency '{value} Hz' not supported" - ) + raise ValueError("Data acquisition frequency '{value} Hz' not supported") if self.mode == MODE_PERIODIC and not self._frequency == value: self._frequency = value self._periodic() From 87218d47e546cf4185235a6340ad30fb37621025 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Sat, 20 Aug 2022 15:06:47 -0400 Subject: [PATCH 3/6] Correct Missing Type Annotations --- adafruit_sht31d.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_sht31d.py b/adafruit_sht31d.py index 6fa737f..fe78516 100644 --- a/adafruit_sht31d.py +++ b/adafruit_sht31d.py @@ -352,7 +352,7 @@ def frequency(self, value: float) -> None: self._frequency = value @property - def temperature(self) -> Union(float, List[float]): + def temperature(self) -> Union[float, List[float]]: """ The measured temperature in degrees Celsius. 'Single' mode reads and returns the current temperature as a float. @@ -365,7 +365,7 @@ def temperature(self) -> Union(float, List[float]): return temperature @property - def relative_humidity(self) -> Union(float, List[float]): + def relative_humidity(self) -> Union[float, List[float]]: """ The measured relative humidity in percent. 'Single' mode reads and returns the current humidity as a float. From b15dd396a2f5c9c2ff761a174f6fb826b19dfacf Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 22 Aug 2022 13:44:36 -0400 Subject: [PATCH 4/6] Correct Missing Type Annotations --- adafruit_sht31d.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/adafruit_sht31d.py b/adafruit_sht31d.py index fe78516..5b91014 100644 --- a/adafruit_sht31d.py +++ b/adafruit_sht31d.py @@ -35,6 +35,8 @@ try: from typing import List, Tuple, Union + from typing_extensions import Literal + from circuitpython_typing import ReadableBuffer from busio import I2C except ImportError: pass @@ -128,7 +130,7 @@ def _crc(data) -> int: return crc & 0xFF -def _unpack(data) -> float: +def _unpack(data: ReadableBuffer) -> List[int]: length = len(data) crc = [None] * (length // 3) word = [None] * (length // 3) @@ -217,7 +219,7 @@ def _periodic(self) -> None: time.sleep(0.001) self._last_read = 0 - def _data(self) -> Tuple[float, float]: + def _data(self) -> Union[Tuple[float, float], Tuple[List[float], List[float]]]: if self.mode == MODE_PERIODIC: data = bytearray(48) data[0] = 0xFF @@ -251,7 +253,7 @@ def _data(self) -> Tuple[float, float]: return temperature[0], humidity[0] return temperature, humidity - def _read(self) -> Tuple[float, float]: + def _read(self) -> Union[Tuple[float, float], Tuple[List[float], List[float]]]: if ( self.mode == MODE_PERIODIC and time.time() > self._last_read + 1 / self.frequency @@ -263,7 +265,7 @@ def _read(self) -> Tuple[float, float]: return self._cached_temperature, self._cached_humidity @property - def mode(self) -> str: + def mode(self) -> Literal["Single", "Periodic"]: """ Operation mode Allowed values are the constants MODE_* @@ -283,7 +285,7 @@ def mode(self, value: str): self._mode = value @property - def repeatability(self) -> Tuple[str, bool, int]: + def repeatability(self) -> Literal["High", "Medium", "Low"]: """ Repeatability Allowed values are the constants REP_* From 32cf5a929e06fcfa071f50c1a74d2e3d2eabfa84 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 22 Aug 2022 14:41:10 -0400 Subject: [PATCH 5/6] Correct Missing Type Annotations --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index a45c547..e6918e2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ Adafruit-Blinka adafruit-circuitpython-busdevice +typing-extensions~=4.0 +adafruit-circuitpython-typing From 11c59a076fd64cd63497cbbe915833a680b43573 Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 22 Aug 2022 20:07:02 -0400 Subject: [PATCH 6/6] Correct Missing Type Annotations --- adafruit_sht31d.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_sht31d.py b/adafruit_sht31d.py index 5b91014..158e6b8 100644 --- a/adafruit_sht31d.py +++ b/adafruit_sht31d.py @@ -274,7 +274,7 @@ def mode(self) -> Literal["Single", "Periodic"]: return self._mode @mode.setter - def mode(self, value: str): + def mode(self, value: Literal["Single", "Periodic"]) -> None: if not value in _SHT31_MODES: raise ValueError(f"Mode '{value}' not supported") if self._mode == MODE_PERIODIC and value != MODE_PERIODIC: @@ -293,7 +293,7 @@ def repeatability(self) -> Literal["High", "Medium", "Low"]: return self._repeatability @repeatability.setter - def repeatability(self, value: Tuple[str, bool, int]) -> None: + def repeatability(self, value: Literal["High", "Medium", "Low"]) -> None: if not value in _SHT31_REP: raise ValueError("Repeatability '{value}' not supported") if self.mode == MODE_PERIODIC and not self._repeatability == value: