Skip to content

Commit b8b3618

Browse files
authored
Merge pull request #19 from tekktrik/feature/add-typing
Add typing
2 parents 832ccf7 + c4d7fe2 commit b8b3618

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

adafruit_74hc595.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@
2626
"""
2727

2828
import digitalio
29-
import adafruit_bus_device.spi_device as spi_device
29+
from adafruit_bus_device import spi_device
30+
31+
try:
32+
import typing # pylint: disable=unused-import
33+
from microcontroller import Pin
34+
import busio
35+
except ImportError:
36+
pass
3037

3138
__version__ = "0.0.0-auto.0"
3239
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_74HC595.git"
@@ -39,7 +46,11 @@ class DigitalInOut:
3946
direction as input will raise an exception.
4047
"""
4148

42-
def __init__(self, pin_number, shift_register_74hc595):
49+
def __init__(
50+
self,
51+
pin_number: Pin,
52+
shift_register_74hc595: "ShiftRegister74HC595",
53+
):
4354
"""Specify the pin number of the shift register (0...7) and
4455
ShiftRegister74HC595 instance.
4556
"""
@@ -53,7 +64,7 @@ def __init__(self, pin_number, shift_register_74hc595):
5364
# is unused by this class). Do not remove them, instead turn off pylint
5465
# in this case.
5566
# pylint: disable=unused-argument
56-
def switch_to_output(self, value=False, **kwargs):
67+
def switch_to_output(self, value: bool = False, **kwargs):
5768
"""``DigitalInOut switch_to_output``"""
5869
self.direction = digitalio.Direction.OUTPUT
5970
self.value = value
@@ -72,7 +83,7 @@ def value(self):
7283
)
7384

7485
@value.setter
75-
def value(self, val):
86+
def value(self, val: bool):
7687

7788
if (
7889
self._pin >= 0
@@ -91,7 +102,7 @@ def direction(self):
91102
return digitalio.Direction.OUTPUT
92103

93104
@direction.setter
94-
def direction(self, val): # pylint: disable=no-self-use
105+
def direction(self, val: digitalio.Direction): # pylint: disable=no-self-use
95106
"""``Direction`` can only be set to ``OUTPUT``."""
96107
if val != digitalio.Direction.OUTPUT:
97108
raise RuntimeError("Digital input not supported.")
@@ -102,7 +113,7 @@ def pull(self):
102113
return None
103114

104115
@pull.setter
105-
def pull(self, val): # pylint: disable=no-self-use
116+
def pull(self, val: digitalio.Pull): # pylint: disable=no-self-use
106117
"""Only supports null/no pull state."""
107118
if val is not None:
108119
raise RuntimeError("Pull-up and pull-down not supported.")
@@ -113,7 +124,12 @@ class ShiftRegister74HC595:
113124
and indicate the number of shift registers being used
114125
"""
115126

116-
def __init__(self, spi, latch, number_of_shift_registers=1):
127+
def __init__(
128+
self,
129+
spi: busio.SPI,
130+
latch: digitalio.DigitalInOut,
131+
number_of_shift_registers: int = 1,
132+
):
117133
self._device = spi_device.SPIDevice(spi, latch, baudrate=1000000)
118134
self._number_of_shift_registers = number_of_shift_registers
119135
self._gpio = bytearray(self._number_of_shift_registers)
@@ -131,14 +147,14 @@ def gpio(self):
131147
return self._gpio
132148

133149
@gpio.setter
134-
def gpio(self, val):
150+
def gpio(self, val: bytearray):
135151
self._gpio = val
136152

137153
with self._device as spi:
138154
# pylint: disable=no-member
139155
spi.write(self._gpio)
140156

141-
def get_pin(self, pin):
157+
def get_pin(self, pin: int) -> Pin:
142158
"""Convenience function to create an instance of the DigitalInOut class
143159
pointing at the specified pin of this 74HC595 device .
144160
"""

0 commit comments

Comments
 (0)