Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing type annotations #29

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions adafruit_hcsr04.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down