Skip to content

Commit 3d68487

Browse files
authored
Merge pull request #26 from JulianOrteil/main
Add and fix existing type hints
2 parents 78b03fa + 0aece2e commit 3d68487

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

adafruit_74hc595.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import typing # pylint: disable=unused-import
3333
from microcontroller import Pin
3434
import busio
35+
from circuitpython_typing import ReadableBuffer
3536
except ImportError:
3637
pass
3738

@@ -46,11 +47,16 @@ class DigitalInOut:
4647
direction as input will raise an exception.
4748
"""
4849

50+
_pin: Pin
51+
_byte_pos: int
52+
_byte_pin: int
53+
_shift_register: "ShiftRegister74HC595"
54+
4955
def __init__(
5056
self,
5157
pin_number: Pin,
5258
shift_register_74hc595: "ShiftRegister74HC595",
53-
):
59+
) -> None:
5460
"""Specify the pin number of the shift register (0...7) and
5561
ShiftRegister74HC595 instance.
5662
"""
@@ -64,27 +70,26 @@ def __init__(
6470
# is unused by this class). Do not remove them, instead turn off pylint
6571
# in this case.
6672
# pylint: disable=unused-argument
67-
def switch_to_output(self, value: bool = False, **kwargs):
73+
def switch_to_output(self, value: bool = False, **kwargs) -> None:
6874
"""``DigitalInOut switch_to_output``"""
6975
self.direction = digitalio.Direction.OUTPUT
7076
self.value = value
7177

72-
def switch_to_input(self, **kwargs): # pylint: disable=no-self-use
78+
def switch_to_input(self, **kwargs) -> None: # pylint: disable=no-self-use
7379
"""``switch_to_input`` is not supported."""
7480
raise RuntimeError("Digital input not supported.")
7581

7682
# pylint: enable=unused-argument
7783

7884
@property
79-
def value(self):
85+
def value(self) -> bool:
8086
"""The value of the pin, either True for high or False for low."""
8187
return self._shift_register.gpio[self._byte_pos] & (1 << self._byte_pin) == (
8288
1 << self._byte_pin
8389
)
8490

8591
@value.setter
86-
def value(self, val: bool):
87-
92+
def value(self, val: bool) -> None:
8893
if (
8994
self._pin >= 0
9095
and self._pin < self._shift_register.number_of_shift_registers * 8
@@ -97,23 +102,26 @@ def value(self, val: bool):
97102
self._shift_register.gpio = gpio
98103

99104
@property
100-
def direction(self):
105+
def direction(self) -> digitalio.Direction.OUTPUT:
101106
"""``Direction`` can only be set to ``OUTPUT``."""
102107
return digitalio.Direction.OUTPUT
103108

104109
@direction.setter
105-
def direction(self, val: digitalio.Direction): # pylint: disable=no-self-use
110+
def direction( # pylint: disable=no-self-use
111+
self,
112+
val: digitalio.Direction.OUTPUT,
113+
) -> None:
106114
"""``Direction`` can only be set to ``OUTPUT``."""
107115
if val != digitalio.Direction.OUTPUT:
108116
raise RuntimeError("Digital input not supported.")
109117

110118
@property
111-
def pull(self):
119+
def pull(self) -> None:
112120
"""Pull-up/down not supported, return None for no pull-up/down."""
113121
return None
114122

115123
@pull.setter
116-
def pull(self, val: digitalio.Pull): # pylint: disable=no-self-use
124+
def pull(self, val: None) -> None: # pylint: disable=no-self-use
117125
"""Only supports null/no pull state."""
118126
if val is not None:
119127
raise RuntimeError("Pull-up and pull-down not supported.")
@@ -124,38 +132,42 @@ class ShiftRegister74HC595:
124132
number of shift registers being used and optional baudrate.
125133
"""
126134

135+
_device: spi_device.SPIDevice
136+
_number_of_shift_registers: int
137+
_gpio: ReadableBuffer
138+
127139
def __init__(
128140
self,
129141
spi: busio.SPI,
130142
latch: digitalio.DigitalInOut,
131143
number_of_shift_registers: int = 1,
132144
baudrate: int = 1000000,
133-
):
145+
) -> None:
134146
self._device = spi_device.SPIDevice(spi, latch, baudrate=baudrate)
135147
self._number_of_shift_registers = number_of_shift_registers
136148
self._gpio = bytearray(self._number_of_shift_registers)
137149

138150
@property
139-
def number_of_shift_registers(self):
151+
def number_of_shift_registers(self) -> int:
140152
"""The number of shift register chips"""
141153
return self._number_of_shift_registers
142154

143155
@property
144-
def gpio(self):
156+
def gpio(self) -> ReadableBuffer:
145157
"""The raw GPIO output register. Each bit represents the
146158
output value of the associated pin (0 = low, 1 = high).
147159
"""
148160
return self._gpio
149161

150162
@gpio.setter
151-
def gpio(self, val: bytearray):
163+
def gpio(self, val: ReadableBuffer) -> None:
152164
self._gpio = val
153165

154166
with self._device as spi:
155167
# pylint: disable=no-member
156168
spi.write(self._gpio)
157169

158-
def get_pin(self, pin: int) -> Pin:
170+
def get_pin(self, pin: int) -> DigitalInOut:
159171
"""Convenience function to create an instance of the DigitalInOut class
160172
pointing at the specified pin of this 74HC595 device .
161173
"""

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-typing

0 commit comments

Comments
 (0)