From b1fcdd8299e63aec240d707adeda32a2283816d7 Mon Sep 17 00:00:00 2001 From: Josh Gadeken Date: Wed, 27 Oct 2021 22:53:44 -0600 Subject: [PATCH] [#19] Add type annotations --- adafruit_pm25/__init__.py | 6 +++--- adafruit_pm25/i2c.py | 15 ++++++++++++--- adafruit_pm25/uart.py | 13 ++++++++++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/adafruit_pm25/__init__.py b/adafruit_pm25/__init__.py index 201fff0..e0d85c7 100644 --- a/adafruit_pm25/__init__.py +++ b/adafruit_pm25/__init__.py @@ -50,7 +50,7 @@ class PM25: """ - def __init__(self): + def __init__(self) -> None: # rad, ok make our internal buffer! self._buffer = bytearray(32) self.aqi_reading = { @@ -68,11 +68,11 @@ def __init__(self): "particles 100um": None, } - def _read_into_buffer(self): + def _read_into_buffer(self) -> None: """Low level buffer filling function, to be overridden""" raise NotImplementedError() - def read(self): + def read(self) -> dict: """Read any available data from the air quality sensor and return a dictionary with available particulate/quality data""" self._read_into_buffer() diff --git a/adafruit_pm25/i2c.py b/adafruit_pm25/i2c.py index 21a2dc8..e163bca 100644 --- a/adafruit_pm25/i2c.py +++ b/adafruit_pm25/i2c.py @@ -35,10 +35,17 @@ # imports import time -from digitalio import Direction +from digitalio import Direction, DigitalInOut from adafruit_bus_device.i2c_device import I2CDevice from . import PM25 +try: + # Used only for typing + import typing # pylint: disable=unused-import + from busio import I2C +except ImportError: + pass + class PM25_I2C(PM25): """ @@ -77,7 +84,9 @@ class PM25_I2C(PM25): """ - def __init__(self, i2c_bus, reset_pin=None, address=0x12): + def __init__( + self, i2c_bus: I2C, reset_pin: DigitalInOut = None, address: int = 0x12 + ) -> None: if reset_pin: # Reset device reset_pin.direction = Direction.OUTPUT @@ -98,7 +107,7 @@ def __init__(self, i2c_bus, reset_pin=None, address=0x12): raise RuntimeError("Unable to find PM2.5 device") super().__init__() - def _read_into_buffer(self): + def _read_into_buffer(self) -> None: with self.i2c_device as i2c: try: i2c.readinto(self._buffer) diff --git a/adafruit_pm25/uart.py b/adafruit_pm25/uart.py index fc28c27..b514cfb 100644 --- a/adafruit_pm25/uart.py +++ b/adafruit_pm25/uart.py @@ -30,9 +30,16 @@ """ import time -from digitalio import Direction +from digitalio import Direction, DigitalInOut from . import PM25 +try: + # Used only for typing + import typing # pylint: disable=unused-import + from busio import UART +except ImportError: + pass + class PM25_UART(PM25): """ @@ -73,7 +80,7 @@ class PM25_UART(PM25): """ - def __init__(self, uart, reset_pin=None): + def __init__(self, uart: UART, reset_pin: DigitalInOut = None): if reset_pin: # Reset device reset_pin.direction = Direction.OUTPUT @@ -86,7 +93,7 @@ def __init__(self, uart, reset_pin=None): self._uart = uart super().__init__() - def _read_into_buffer(self): + def _read_into_buffer(self) -> None: while True: b = self._uart.read(1) if not b: