From 42a28662103b48ac0d872826e48e6904bf6464bb Mon Sep 17 00:00:00 2001 From: Thomas Franks Date: Mon, 12 Sep 2022 13:07:04 -0400 Subject: [PATCH] Add missing type annotations --- adafruit_hcsr04.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/adafruit_hcsr04.py b/adafruit_hcsr04.py index 8f2d8bf..8cdcb00 100644 --- a/adafruit_hcsr04.py +++ b/adafruit_hcsr04.py @@ -32,6 +32,13 @@ import time from digitalio import DigitalInOut, Direction +try: + from typing import Optional, Type + from types import TracebackType + from microcontroller import Pin +except ImportError: + pass + _USE_PULSEIO = False try: from pulseio import PulseIn @@ -68,7 +75,9 @@ class HCSR04: time.sleep(0.1) """ - def __init__(self, trigger_pin, echo_pin, *, timeout=0.1): + def __init__( + self, trigger_pin: Pin, echo_pin: Pin, *, timeout: float = 0.1 + ) -> None: """ :param trigger_pin: The pin on the microcontroller that's connected to the ``Trig`` pin on the HC-SR04. @@ -92,21 +101,26 @@ def __init__(self, trigger_pin, echo_pin, *, timeout=0.1): self._echo = DigitalInOut(echo_pin) self._echo.direction = Direction.INPUT - def __enter__(self): + def __enter__(self) -> "HCSR04": """Allows for use in context managers.""" return self - def __exit__(self, exc_type, exc_val, exc_tb): + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> None: """Automatically de-initialize after a context manager.""" self.deinit() - def deinit(self): + def deinit(self) -> None: """De-initialize the trigger and echo pins.""" self._trig.deinit() self._echo.deinit() @property - def distance(self): + def distance(self) -> float: """Return the distance measured by the sensor in cm. This is the function that will be called most often in user code. The @@ -126,7 +140,7 @@ def distance(self): """ return self._dist_two_wire() # at this time we only support 2-wire meausre - def _dist_two_wire(self): + def _dist_two_wire(self) -> float: if _USE_PULSEIO: self._echo.clear() # Discard any previous pulse values self._trig.value = True # Set trig high