Skip to content

Commit 93d1c7d

Browse files
authored
Merge pull request #14 from tcfranks/main
Add Missing Type Annotations
2 parents 1f67014 + 2319df6 commit 93d1c7d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

adafruit_tfmini.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
import time
2626
import struct
2727

28+
try:
29+
import typing # pylint: disable=unused-import
30+
from typing_extensions import Literal
31+
from circuitpython_typing import ReadableBuffer
32+
from busio import UART
33+
except ImportError:
34+
pass
35+
2836
__version__ = "0.0.0+auto.0"
2937
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TFmini.git"
3038

@@ -46,7 +54,7 @@ class TFmini:
4654
:param timeout: how long we'll wait for valid data or response, in seconds. Default is 1
4755
"""
4856

49-
def __init__(self, uart, *, timeout=1):
57+
def __init__(self, uart: UART, *, timeout: float = 1) -> None:
5058
self._uart = uart
5159
self._uart.baudrate = 115200
5260
self._uart.reset_input_buffer()
@@ -55,7 +63,7 @@ def __init__(self, uart, *, timeout=1):
5563
self._mode = None
5664

5765
@property
58-
def distance(self):
66+
def distance(self) -> int:
5967
"""The most recent distance measurement in centimeters"""
6068
try:
6169
self._uart.reset_input_buffer()
@@ -87,24 +95,24 @@ def distance(self):
8795
raise RuntimeError("Timed out looking for valid data")
8896

8997
@property
90-
def strength(self):
98+
def strength(self) -> int:
9199
"""The signal validity, higher value means better measurement"""
92100
_ = self.distance # trigger distance measurement
93101
return self._strength
94102

95103
@property
96-
def mode(self):
104+
def mode(self) -> Literal[2, 7]:
97105
"""The measurement mode can be MODE_SHORT (2) or MODE_LONG (7)"""
98106
_ = self.distance # trigger distance measurement
99107
return self._mode
100108

101109
@mode.setter
102-
def mode(self, newmode):
110+
def mode(self, newmode: Literal[2, 7]) -> None:
103111
if not newmode in (MODE_LONG, MODE_SHORT):
104112
raise ValueError("Invalid mode")
105113
self._set_config(_CONFIGPARAM + bytes([0, 0, newmode, 0x11]))
106114

107-
def _set_config(self, command):
115+
def _set_config(self, command: ReadableBuffer) -> None:
108116
"""Manager for sending commands, put sensor into config mode, config,
109117
then exit configuration mode!"""
110118
self._uart.write(_STARTCONFIG)

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
pyserial
7+
adafruit-circuitpython-typing
8+
typing-extensions~=4.0

0 commit comments

Comments
 (0)