Skip to content

Commit 536015c

Browse files
committed
Add Missing Type Annotations
1 parent ae1e759 commit 536015c

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

adafruit_ds2413.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
from micropython import const
1919
from adafruit_onewire.device import OneWireDevice
2020

21+
try:
22+
from typing import Union
23+
from typing_extensions import Literal
24+
from adafruit_onewire.bus import OneWireBus # pylint: disable=ungrouped-imports
25+
from microcontroller import Pin
26+
except ImportError:
27+
pass
28+
2129
_DS2413_ACCESS_READ = b"\xF5"
2230
_DS2413_ACCESS_WRITE = b"\x5A"
2331
_DS2413_ACK_SUCCESS = b"\xAA"
@@ -29,7 +37,9 @@
2937
class DS2413Pin:
3038
"""Class which provides interface to single DS2413 GPIO pin."""
3139

32-
def __init__(self, number, host, direction=OUTPUT):
40+
def __init__(
41+
self, number: Literal[0, 1], host, direction: Literal[0, 1] = OUTPUT
42+
) -> None:
3343
if number not in (0, 1):
3444
raise ValueError("Incorrect pin number.")
3545
self._number = number
@@ -39,20 +49,20 @@ def __init__(self, number, host, direction=OUTPUT):
3949
self.direction = direction # set it through setter
4050

4151
@property
42-
def direction(self):
52+
def direction(self) -> Literal[0, 1]:
4353
"""The direction of the pin, either INPUT or OUTPUT."""
4454
return self._direction
4555

4656
@direction.setter
47-
def direction(self, direction):
57+
def direction(self, direction: Literal[0, 1]) -> None:
4858
if direction not in (INPUT, OUTPUT):
4959
raise ValueError("Incorrect direction setting.")
5060
self._direction = OUTPUT
5161
self.value = False
5262
self._direction = direction
5363

5464
@property
55-
def value(self):
65+
def value(self) -> bool:
5666
"""The pin state if configured as INPUT. The output latch state
5767
if configured as OUTPUT. True is HIGH/ON, False is LOW/OFF."""
5868
# return Pin State if configured for INPUT
@@ -61,7 +71,7 @@ def value(self):
6171
return not self._host.pio_state & (self._mask << self._direction)
6272

6373
@value.setter
64-
def value(self, state):
74+
def value(self, state: Union[bool, Literal[0, 1]]) -> None:
6575
# This only makes sense if the pin is configured for OUTPUT.
6676
if self._direction == INPUT:
6777
raise RuntimeError("Can't set value when pin is set to input.")
@@ -84,7 +94,7 @@ def value(self, state):
8494
class DS2413:
8595
"""Class which provides interface to DS2413 GPIO breakout."""
8696

87-
def __init__(self, bus, address):
97+
def __init__(self, bus: OneWireBus, address: int) -> None:
8898
if address.family_code == 0x3A:
8999
self._address = address
90100
self._device = OneWireDevice(bus, address)
@@ -95,35 +105,35 @@ def __init__(self, bus, address):
95105
raise RuntimeError("Incorrect family code in device address.")
96106

97107
@property
98-
def IOA(self):
108+
def IOA(self) -> Pin:
99109
"""The pin object for channel A."""
100110
if self._IOA is None:
101111
self._IOA = DS2413Pin(0, self)
102112
return self._IOA
103113

104114
@property
105-
def IOB(self):
115+
def IOB(self) -> Pin:
106116
"""The pin object for channel B."""
107117
if self._IOB is None:
108118
self._IOB = DS2413Pin(1, self)
109119
return self._IOB
110120

111121
@property
112-
def pio_state(self):
122+
def pio_state(self) -> int:
113123
"""The state of both PIO channels."""
114124
return self._read_status()
115125

116126
@pio_state.setter
117-
def pio_state(self, value):
127+
def pio_state(self, value: int) -> None:
118128
return self._write_latches(value)
119129

120-
def _read_status(self):
130+
def _read_status(self) -> int:
121131
with self._device as dev:
122132
dev.write(_DS2413_ACCESS_READ)
123133
dev.readinto(self._buf, end=1)
124134
return self._buf[0]
125135

126-
def _write_latches(self, value):
136+
def _write_latches(self, value: int) -> None:
127137
# top six bits must be 1
128138
value |= 0xFC
129139
self._buf[0] = value

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-onewire
77
adafruit-circuitpython-busdevice
8+
typing-extensions~=4.0

0 commit comments

Comments
 (0)