Skip to content

Commit 70e949f

Browse files
committed
Add type hints
Using digitalio.DigitalInOut long form where appropriate so it's not confused with the library's custom version of it, and doesn't have an impact on existing code
1 parent fc0272f commit 70e949f

File tree

9 files changed

+127
-74
lines changed

9 files changed

+127
-74
lines changed

adafruit_mcp230xx/digital_inout.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,26 @@
1414

1515
import digitalio
1616

17+
try:
18+
import typing # pylint: disable=unused-import
19+
from adafruit_mcp230xx.mcp230xx import MCP230XX
20+
from digitalio import Pull, Direction
21+
except ImportError:
22+
pass
23+
1724
__version__ = "0.0.0-auto.0"
1825
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
1926

2027
# Internal helpers to simplify setting and getting a bit inside an integer.
21-
def _get_bit(val, bit):
28+
def _get_bit(val, bit: int) -> int:
2229
return val & (1 << bit) > 0
2330

2431

25-
def _enable_bit(val, bit):
32+
def _enable_bit(val, bit: int) -> int:
2633
return val | (1 << bit)
2734

2835

29-
def _clear_bit(val, bit):
36+
def _clear_bit(val, bit: int) -> int:
3037
return val & ~(1 << bit)
3138

3239

@@ -41,7 +48,7 @@ class DigitalInOut:
4148
configurations.
4249
"""
4350

44-
def __init__(self, pin_number, mcp230xx):
51+
def __init__(self, pin_number: int, mcp230xx: MCP230XX):
4552
"""Specify the pin number of the MCP230xx (0...7 for MCP23008, or 0...15
4653
for MCP23017) and MCP23008 instance.
4754
"""
@@ -53,14 +60,14 @@ def __init__(self, pin_number, mcp230xx):
5360
# is unused by this class). Do not remove them, instead turn off pylint
5461
# in this case.
5562
# pylint: disable=unused-argument
56-
def switch_to_output(self, value=False, **kwargs):
63+
def switch_to_output(self, value: bool = False, **kwargs):
5764
"""Switch the pin state to a digital output with the provided starting
5865
value (True/False for high or low, default is False/low).
5966
"""
6067
self.direction = digitalio.Direction.OUTPUT
6168
self.value = value
6269

63-
def switch_to_input(self, pull=None, invert_polarity=False, **kwargs):
70+
def switch_to_input(self, pull: Pull = None, invert_polarity: bool = False, **kwargs):
6471
"""Switch the pin state to a digital input with the provided starting
6572
pull-up resistor state (optional, no pull-up by default) and input polarity. Note that
6673
pull-down resistors are NOT supported!
@@ -80,7 +87,7 @@ def value(self):
8087
return _get_bit(self._mcp.gpio, self._pin)
8188

8289
@value.setter
83-
def value(self, val):
90+
def value(self, val: bool):
8491
if val:
8592
self._mcp.gpio = _enable_bit(self._mcp.gpio, self._pin)
8693
else:
@@ -96,7 +103,7 @@ def direction(self):
96103
return digitalio.Direction.OUTPUT
97104

98105
@direction.setter
99-
def direction(self, val):
106+
def direction(self, val: Direction):
100107
if val == digitalio.Direction.INPUT:
101108
self._mcp.iodir = _enable_bit(self._mcp.iodir, self._pin)
102109
elif val == digitalio.Direction.OUTPUT:
@@ -119,7 +126,7 @@ def pull(self):
119126
return None
120127

121128
@pull.setter
122-
def pull(self, val):
129+
def pull(self, val: Pull):
123130
try:
124131
if val is None:
125132
self._mcp.gppu = _clear_bit(self._mcp.gppu, self._pin)
@@ -143,7 +150,7 @@ def invert_polarity(self):
143150
return False
144151

145152
@invert_polarity.setter
146-
def invert_polarity(self, val):
153+
def invert_polarity(self, val: bool):
147154
if val:
148155
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
149156
else:

adafruit_mcp230xx/mcp23008.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
from .mcp230xx import MCP230XX
1717
from .digital_inout import DigitalInOut
1818

19+
try:
20+
import typing # pylint: disable=unused-import
21+
from busio import I2C
22+
except ImportError:
23+
pass
24+
1925
__version__ = "0.0.0-auto.0"
2026
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
2127

@@ -37,7 +43,7 @@ class MCP23008(MCP230XX):
3743
at the specified I2C address.
3844
"""
3945

40-
def __init__(self, i2c, address=_MCP23008_ADDRESS, reset=True):
46+
def __init__(self, i2c: I2C, address: int = _MCP23008_ADDRESS, reset: bool = True):
4147
super().__init__(i2c, address)
4248

4349
if reset:
@@ -55,7 +61,7 @@ def gpio(self):
5561
return self._read_u8(_MCP23008_GPIO)
5662

5763
@gpio.setter
58-
def gpio(self, val):
64+
def gpio(self, val: int):
5965
self._write_u8(_MCP23008_GPIO, val)
6066

6167
@property
@@ -66,7 +72,7 @@ def iodir(self):
6672
return self._read_u8(_MCP23008_IODIR)
6773

6874
@iodir.setter
69-
def iodir(self, val):
75+
def iodir(self, val: int):
7076
self._write_u8(_MCP23008_IODIR, val)
7177

7278
@property
@@ -78,10 +84,10 @@ def gppu(self):
7884
return self._read_u8(_MCP23008_GPPU)
7985

8086
@gppu.setter
81-
def gppu(self, val):
87+
def gppu(self, val: int):
8288
self._write_u8(_MCP23008_GPPU, val)
8389

84-
def get_pin(self, pin):
90+
def get_pin(self, pin: int) -> DigitalInOut:
8591
"""Convenience function to create an instance of the DigitalInOut class
8692
pointing at the specified pin of this MCP23008 device.
8793
"""

adafruit_mcp230xx/mcp23016.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
from .mcp230xx import MCP230XX
2525
from .digital_inout import DigitalInOut
2626

27+
try:
28+
import typing # pylint: disable=unused-import
29+
from busio import I2C
30+
except ImportError:
31+
pass
32+
2733
__version__ = "0.0.0-auto.0"
2834
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
2935

@@ -45,7 +51,7 @@ class MCP23016(MCP230XX):
4551
at the specified I2C address.
4652
"""
4753

48-
def __init__(self, i2c, address=_MCP23016_ADDRESS, reset=True):
54+
def __init__(self, i2c: I2C, address: int = _MCP23016_ADDRESS, reset: bool = True):
4955
super().__init__(i2c, address)
5056

5157
if reset:
@@ -62,7 +68,7 @@ def gpio(self):
6268
return self._read_u16le(_MCP23016_GPIO0)
6369

6470
@gpio.setter
65-
def gpio(self, val):
71+
def gpio(self, val: int):
6672
self._write_u16le(_MCP23016_GPIO0, val)
6773

6874
@property
@@ -74,7 +80,7 @@ def gpioa(self):
7480
return self._read_u8(_MCP23016_GPIO0)
7581

7682
@gpioa.setter
77-
def gpioa(self, val):
83+
def gpioa(self, val: int):
7884
self._write_u8(_MCP23016_GPIO0, val)
7985

8086
@property
@@ -86,7 +92,7 @@ def gpiob(self):
8692
return self._read_u8(_MCP23016_GPIO1)
8793

8894
@gpiob.setter
89-
def gpiob(self, val):
95+
def gpiob(self, val: int):
9096
self._write_u8(_MCP23016_GPIO1, val)
9197

9298
@property
@@ -97,7 +103,7 @@ def iodir(self):
97103
return self._read_u16le(_MCP23016_IODIR0)
98104

99105
@iodir.setter
100-
def iodir(self, val):
106+
def iodir(self, val: int):
101107
self._write_u16le(_MCP23016_IODIR0, val)
102108

103109
@property
@@ -108,7 +114,7 @@ def iodira(self):
108114
return self._read_u8(_MCP23016_IODIR0)
109115

110116
@iodira.setter
111-
def iodira(self, val):
117+
def iodira(self, val: int):
112118
self._write_u8(_MCP23016_IODIR0, val)
113119

114120
@property
@@ -119,10 +125,10 @@ def iodirb(self):
119125
return self._read_u8(_MCP23016_IODIR1)
120126

121127
@iodirb.setter
122-
def iodirb(self, val):
128+
def iodirb(self, val: int):
123129
self._write_u8(_MCP23016_IODIR1, val)
124130

125-
def get_pin(self, pin):
131+
def get_pin(self, pin: int) -> DigitalInOut:
126132
"""Convenience function to create an instance of the DigitalInOut class
127133
pointing at the specified pin of this MCP23016 device.
128134
"""

adafruit_mcp230xx/mcp23017.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
from .mcp230xx import MCP230XX
1919
from .digital_inout import DigitalInOut
2020

21+
try:
22+
import typing # pylint: disable=unused-import
23+
from busio import I2C
24+
except ImportError:
25+
pass
26+
2127
__version__ = "0.0.0-auto.0"
2228
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
2329

@@ -45,7 +51,7 @@ class MCP23017(MCP230XX):
4551
at the specified I2C address.
4652
"""
4753

48-
def __init__(self, i2c, address=_MCP23017_ADDRESS, reset=True):
54+
def __init__(self, i2c: I2C, address: int = _MCP23017_ADDRESS, reset: bool = True):
4955
super().__init__(i2c, address)
5056
if reset:
5157
# Reset to all inputs with no pull-ups and no inverted polarity.
@@ -63,7 +69,7 @@ def gpio(self):
6369
return self._read_u16le(_MCP23017_GPIOA)
6470

6571
@gpio.setter
66-
def gpio(self, val):
72+
def gpio(self, val: int):
6773
self._write_u16le(_MCP23017_GPIOA, val)
6874

6975
@property
@@ -75,7 +81,7 @@ def gpioa(self):
7581
return self._read_u8(_MCP23017_GPIOA)
7682

7783
@gpioa.setter
78-
def gpioa(self, val):
84+
def gpioa(self, val: int):
7985
self._write_u8(_MCP23017_GPIOA, val)
8086

8187
@property
@@ -87,7 +93,7 @@ def gpiob(self):
8793
return self._read_u8(_MCP23017_GPIOB)
8894

8995
@gpiob.setter
90-
def gpiob(self, val):
96+
def gpiob(self, val: int):
9197
self._write_u8(_MCP23017_GPIOB, val)
9298

9399
@property
@@ -98,7 +104,7 @@ def iodir(self):
98104
return self._read_u16le(_MCP23017_IODIRA)
99105

100106
@iodir.setter
101-
def iodir(self, val):
107+
def iodir(self, val: int):
102108
self._write_u16le(_MCP23017_IODIRA, val)
103109

104110
@property
@@ -109,7 +115,7 @@ def iodira(self):
109115
return self._read_u8(_MCP23017_IODIRA)
110116

111117
@iodira.setter
112-
def iodira(self, val):
118+
def iodira(self, val: int):
113119
self._write_u8(_MCP23017_IODIRA, val)
114120

115121
@property
@@ -120,7 +126,7 @@ def iodirb(self):
120126
return self._read_u8(_MCP23017_IODIRB)
121127

122128
@iodirb.setter
123-
def iodirb(self, val):
129+
def iodirb(self, val: int):
124130
self._write_u8(_MCP23017_IODIRB, val)
125131

126132
@property
@@ -132,7 +138,7 @@ def gppu(self):
132138
return self._read_u16le(_MCP23017_GPPUA)
133139

134140
@gppu.setter
135-
def gppu(self, val):
141+
def gppu(self, val: int):
136142
self._write_u16le(_MCP23017_GPPUA, val)
137143

138144
@property
@@ -144,7 +150,7 @@ def gppua(self):
144150
return self._read_u8(_MCP23017_GPPUA)
145151

146152
@gppua.setter
147-
def gppua(self, val):
153+
def gppua(self, val: int):
148154
self._write_u8(_MCP23017_GPPUA, val)
149155

150156
@property
@@ -156,10 +162,10 @@ def gppub(self):
156162
return self._read_u8(_MCP23017_GPPUB)
157163

158164
@gppub.setter
159-
def gppub(self, val):
165+
def gppub(self, val: int):
160166
self._write_u8(_MCP23017_GPPUB, val)
161167

162-
def get_pin(self, pin):
168+
def get_pin(self, pin: int) -> DigitalInOut:
163169
"""Convenience function to create an instance of the DigitalInOut class
164170
pointing at the specified pin of this MCP23017 device.
165171
"""
@@ -176,7 +182,7 @@ def ipol(self):
176182
return self._read_u16le(_MCP23017_IPOLA)
177183

178184
@ipol.setter
179-
def ipol(self, val):
185+
def ipol(self, val: int):
180186
self._write_u16le(_MCP23017_IPOLA, val)
181187

182188
@property
@@ -188,7 +194,7 @@ def ipola(self):
188194
return self._read_u8(_MCP23017_IPOLA)
189195

190196
@ipola.setter
191-
def ipola(self, val):
197+
def ipola(self, val: int):
192198
self._write_u8(_MCP23017_IPOLA, val)
193199

194200
@property
@@ -200,7 +206,7 @@ def ipolb(self):
200206
return self._read_u8(_MCP23017_IPOLB)
201207

202208
@ipolb.setter
203-
def ipolb(self, val):
209+
def ipolb(self, val: int):
204210
self._write_u8(_MCP23017_IPOLB, val)
205211

206212
@property
@@ -215,7 +221,7 @@ def interrupt_configuration(self):
215221
return self._read_u16le(_MCP23017_INTCONA)
216222

217223
@interrupt_configuration.setter
218-
def interrupt_configuration(self, val):
224+
def interrupt_configuration(self, val: int):
219225
self._write_u16le(_MCP23017_INTCONA, val)
220226

221227
@property
@@ -229,7 +235,7 @@ def interrupt_enable(self):
229235
return self._read_u16le(_MCP23017_GPINTENA)
230236

231237
@interrupt_enable.setter
232-
def interrupt_enable(self, val):
238+
def interrupt_enable(self, val: int):
233239
self._write_u16le(_MCP23017_GPINTENA, val)
234240

235241
@property
@@ -242,7 +248,7 @@ def default_value(self):
242248
return self._read_u16le(_MCP23017_DEFVALA)
243249

244250
@default_value.setter
245-
def default_value(self, val):
251+
def default_value(self, val: int):
246252
self._write_u16le(_MCP23017_DEFVALA, val)
247253

248254
@property
@@ -258,7 +264,7 @@ def io_control(self):
258264
return self._read_u8(_MCP23017_IOCON)
259265

260266
@io_control.setter
261-
def io_control(self, val):
267+
def io_control(self, val: int):
262268
val &= ~0x80
263269
self._write_u8(_MCP23017_IOCON, val)
264270

0 commit comments

Comments
 (0)