Skip to content

Commit 08e2843

Browse files
authored
Merge pull request #74 from tekktrik/feature/add-typehints
Add typehints
2 parents 8f3396b + 3a3fc72 commit 08e2843

22 files changed

+248
-103
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
name: pylint (library code)
2525
types: [python]
2626
args:
27-
- --disable=consider-using-f-string
27+
- --disable=consider-using-f-string,duplicate-code
2828
exclude: "^(docs/|examples/|tests/|setup.py$)"
2929
- id: pylint
3030
name: pylint (example code)

adafruit_featherwing/alphanum_featherwing.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@
1515
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
1616

1717
import board
18-
import adafruit_ht16k33.segments as segments
18+
from adafruit_ht16k33 import segments
1919
from adafruit_featherwing.led_segments import Segments
2020

21+
try:
22+
from typing import Optional
23+
from busio import I2C
24+
except ImportError:
25+
pass
26+
2127

2228
class AlphaNumFeatherWing(Segments):
2329
"""Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing
2430
<https://www.adafruit.com/product/3139>`_.
2531
2632
Automatically uses the feather's I2C bus."""
2733

28-
def __init__(self, address=0x70, i2c=None):
34+
def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None):
2935
super().__init__()
3036
if i2c is None:
3137
i2c = board.I2C()

adafruit_featherwing/auto_writeable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def auto_write(self):
2727
return self._auto_write
2828

2929
@auto_write.setter
30-
def auto_write(self, write):
30+
def auto_write(self, write: bool):
3131
if isinstance(write, bool):
3232
self._auto_write = write

adafruit_featherwing/dotstar_featherwing.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
import adafruit_dotstar as dotstar
1919
from adafruit_featherwing.pixelmatrix import PixelMatrix
2020

21+
try:
22+
import typing # pylint: disable=unused-import
23+
from microcontroller import Pin
24+
except ImportError:
25+
pass
26+
2127

2228
class DotStarFeatherWing(PixelMatrix):
2329
"""Class representing a `DotStar FeatherWing
2430
<https://www.adafruit.com/product/3449>`_.
2531
2632
The feather uses pins D13 and D11"""
2733

28-
def __init__(self, clock=board.D13, data=board.D11, brightness=0.2):
34+
def __init__(
35+
self, clock: Pin = board.D13, data: Pin = board.D11, brightness: float = 0.2
36+
):
2937
"""
3038
:param pin clock: The clock pin for the featherwing
3139
:param pin data: The data pin for the featherwing

adafruit_featherwing/gps_featherwing.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@
1818
import busio
1919
import adafruit_gps
2020

21+
try:
22+
from typing import Optional
23+
except ImportError:
24+
pass
25+
2126

2227
class GPSFeatherWing:
2328
"""Class representing an `Ultimate GPS FeatherWing
2429
<https://www.adafruit.com/product/3133>`_.
2530
2631
Automatically uses the feather's UART bus."""
2732

28-
def __init__(self, update_period=1000, baudrate=9600):
33+
def __init__(self, update_period: int = 1000, baudrate: int = 9600):
2934
"""
3035
:param int update_period: (Optional) The amount of time in milliseconds between
3136
updates (default=1000)
@@ -36,8 +41,7 @@ def __init__(self, update_period=1000, baudrate=9600):
3641
if update_period < 250:
3742
raise ValueError("Update Frequency be at least 250 milliseconds")
3843
timeout = update_period // 1000 + 2
39-
if timeout < 3:
40-
timeout = 3
44+
timeout = max(timeout, 3)
4145

4246
self._uart = busio.UART(board.TX, board.RX, baudrate=baudrate, timeout=timeout)
4347
self._gps = adafruit_gps.GPS(self._uart, debug=False)
@@ -47,7 +51,7 @@ def __init__(self, update_period=1000, baudrate=9600):
4751
)
4852
self._gps.send_command(bytes("PMTK220,{}".format(update_period), "utf-8"))
4953

50-
def update(self):
54+
def update(self) -> bool:
5155
"""
5256
Make sure to call ``gps.update()`` every loop iteration and at least twice
5357
as fast as data comes from the GPS unit (usually every second).
@@ -57,7 +61,7 @@ def update(self):
5761
"""
5862
return self._gps.update()
5963

60-
def read(self, size):
64+
def read(self, size: int) -> Optional[bytearray]:
6165
"""
6266
Read the UART for any information that may be on it
6367
@@ -69,7 +73,7 @@ def read(self, size):
6973
return self._uart.read(size)
7074
return None
7175

72-
def send_command(self, command):
76+
def send_command(self, command: bytearray):
7377
"""
7478
Send a bytearray command to the GPS module
7579

adafruit_featherwing/ina219_featherwing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
import board
1818
import adafruit_ina219
1919

20+
try:
21+
from typing import Optional
22+
from busio import I2C
23+
except ImportError:
24+
pass
25+
2026

2127
class INA219FeatherWing:
2228
"""Class representing an `Adafruit INA219 FeatherWing
2329
<https://www.adafruit.com/product/3650>`_.
2430
2531
Automatically uses the feather's I2C bus."""
2632

27-
def __init__(self, i2c=None):
33+
def __init__(self, i2c: Optional[I2C] = None):
2834
if i2c is None:
2935
i2c = board.I2C()
3036
self._ina219 = adafruit_ina219.INA219(i2c)

adafruit_featherwing/joy_featherwing.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
from micropython import const
1919
import adafruit_seesaw.seesaw
2020

21+
try:
22+
from typing import Optional, Tuple
23+
from busio import I2C
24+
except ImportError:
25+
pass
26+
27+
2128
BUTTON_A = const(1 << 6)
2229
BUTTON_B = const(1 << 7)
2330
BUTTON_Y = const(1 << 9)
@@ -30,7 +37,7 @@ class JoyFeatherWing:
3037
3138
Automatically uses the feather's I2C bus."""
3239

33-
def __init__(self, i2c=None):
40+
def __init__(self, i2c: Optional[I2C] = None):
3441
if i2c is None:
3542
i2c = board.I2C()
3643
self._seesaw = adafruit_seesaw.seesaw.Seesaw(i2c)
@@ -157,7 +164,7 @@ def button_select(self):
157164
"""
158165
return self._check_button(BUTTON_SELECT)
159166

160-
def _check_button(self, button):
167+
def _check_button(self, button: int) -> bool:
161168
"""Utilises the seesaw to determine which button is being pressed."""
162169
buttons = self._seesaw.digital_read_bulk(button)
163170
return not buttons != 0
@@ -200,7 +207,7 @@ def joystick_offset(self):
200207
return self._joystick_offset
201208

202209
@joystick_offset.setter
203-
def joystick_offset(self, offset):
210+
def joystick_offset(self, offset: Tuple[int, int]):
204211
self._joystick_offset = offset
205212

206213
def zero_joystick(self):

adafruit_featherwing/keyboard_featherwing.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
# pylint: disable-msg=too-many-arguments
3030
from adafruit_featherwing.tft_featherwing import TFTFeatherWing
3131

32+
try:
33+
from typing import Optional
34+
from busio import SPI, I2C
35+
from microcontroller import Pin
36+
except ImportError:
37+
pass
38+
3239

3340
class KeyboardFeatherwing(TFTFeatherWing):
3441
"""Class representing a `Keyboard Featherwing`
@@ -38,13 +45,13 @@ class KeyboardFeatherwing(TFTFeatherWing):
3845

3946
def __init__(
4047
self,
41-
spi=None,
42-
cs=None,
43-
dc=None,
44-
i2c=None,
45-
ts_cs=None,
46-
sd_cs=None,
47-
neopixel_pin=None,
48+
spi: Optional[SPI] = None,
49+
cs: Optional[Pin] = None,
50+
dc: Optional[Pin] = None,
51+
i2c: Optional[I2C] = None,
52+
ts_cs: Optional[Pin] = None,
53+
sd_cs: Optional[Pin] = None,
54+
neopixel_pin: Optional[Pin] = None,
4855
):
4956
super().__init__(spi, cs, dc, ts_cs, sd_cs)
5057

adafruit_featherwing/led_segments.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
# pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation
1818

19+
try:
20+
from typing import Union
21+
except ImportError:
22+
pass
23+
1924

2025
class Segments:
2126
"""Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing
@@ -26,7 +31,7 @@ class Segments:
2631
def __init__(self):
2732
self._segments = None
2833

29-
def print(self, value):
34+
def print(self, value: Union[str, int]):
3035
"""
3136
Print a number or text to the display
3237
@@ -41,7 +46,7 @@ def print(self, value):
4146
self._segments.print(value)
4247
self._segments.show()
4348

44-
def marquee(self, text, delay=0.25, loop=True):
49+
def marquee(self, text: str, delay: float = 0.25, loop: bool = True):
4550
"""
4651
Automatically scroll the text at the specified delay between characters
4752
@@ -53,7 +58,7 @@ def marquee(self, text, delay=0.25, loop=True):
5358
"""
5459
self._segments.marquee(text, delay, loop)
5560

56-
def fill(self, fill):
61+
def fill(self, fill: bool):
5762
"""Change all Segments on or off
5863
5964
:param bool fill: True turns all segments on, False turns all segments off
@@ -75,7 +80,7 @@ def blink_rate(self):
7580
return self._segments.blink_rate
7681

7782
@blink_rate.setter
78-
def blink_rate(self, rate):
83+
def blink_rate(self, rate: int):
7984
self._segments.blink_rate = rate
8085

8186
@property
@@ -87,7 +92,7 @@ def brightness(self):
8792
return round(self._segments.brightness * 15)
8893

8994
@brightness.setter
90-
def brightness(self, brightness):
95+
def brightness(self, brightness: int):
9196
if not 0 <= brightness <= 15:
9297
raise ValueError("Brightness must be a value between 0 and 15")
9398
self._segments.brightness = brightness / 15

adafruit_featherwing/matrix_featherwing.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@
1616
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
1717

1818
import board
19-
import adafruit_ht16k33.matrix as matrix
19+
from adafruit_ht16k33 import matrix
2020

2121
from adafruit_featherwing.auto_writeable import AutoWriteable
2222

23+
try:
24+
from typing import Optional, Tuple, Union
25+
from busio import I2C
26+
except ImportError:
27+
pass
28+
2329

2430
class MatrixFeatherWing(AutoWriteable):
2531
"""Class representing an `Adafruit 8x16 LED Matrix FeatherWing
2632
<https://www.adafruit.com/product/3155>`_.
2733
2834
Automatically uses the feather's I2C bus."""
2935

30-
def __init__(self, address=0x70, i2c=None):
36+
def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None):
3137

3238
if i2c is None:
3339
i2c = board.I2C()
@@ -37,14 +43,14 @@ def __init__(self, address=0x70, i2c=None):
3743
self.rows = 8
3844
super().__init__()
3945

40-
def __getitem__(self, key):
46+
def __getitem__(self, key: Tuple[int, int]) -> bool:
4147
"""
4248
Get the current value of a pixel
4349
"""
4450
x, y = key
4551
return self.pixel(x, y)
4652

47-
def __setitem__(self, key, value):
53+
def __setitem__(self, key: Tuple[int, int], value: Union[int, bool]):
4854
"""
4955
Turn a pixel off or on
5056
"""
@@ -59,7 +65,7 @@ def _update(self):
5965
if self._auto_write:
6066
self._matrix.show()
6167

62-
def pixel(self, x, y, color=None):
68+
def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
6369
"""
6470
Turn a pixel on or off or retrieve a pixel value
6571
@@ -79,7 +85,7 @@ def show(self):
7985
"""
8086
self._matrix.show()
8187

82-
def fill(self, fill):
88+
def fill(self, fill: bool):
8389
"""
8490
Turn all pixels on or off
8591
@@ -92,7 +98,7 @@ def fill(self, fill):
9298
else:
9399
raise ValueError("Must set to either True or False.")
94100

95-
def shift_right(self, rotate=False):
101+
def shift_right(self, rotate: bool = False):
96102
"""
97103
Shift all pixels right
98104
@@ -101,7 +107,7 @@ def shift_right(self, rotate=False):
101107
self._matrix.shift_right(rotate)
102108
self._update()
103109

104-
def shift_left(self, rotate=False):
110+
def shift_left(self, rotate: bool = False):
105111
"""
106112
Shift all pixels left
107113
@@ -110,7 +116,7 @@ def shift_left(self, rotate=False):
110116
self._matrix.shift_left(rotate)
111117
self._update()
112118

113-
def shift_up(self, rotate=False):
119+
def shift_up(self, rotate: bool = False):
114120
"""
115121
Shift all pixels up
116122
@@ -119,7 +125,7 @@ def shift_up(self, rotate=False):
119125
self._matrix.shift_up(rotate)
120126
self._update()
121127

122-
def shift_down(self, rotate=False):
128+
def shift_down(self, rotate: bool = False):
123129
"""
124130
Shift all pixels down
125131
@@ -138,7 +144,7 @@ def blink_rate(self):
138144
return self._matrix.blink_rate
139145

140146
@blink_rate.setter
141-
def blink_rate(self, rate):
147+
def blink_rate(self, rate: int):
142148
self._matrix.blink_rate = rate
143149

144150
@property
@@ -150,7 +156,7 @@ def brightness(self):
150156
return round(self._matrix.brightness * 15)
151157

152158
@brightness.setter
153-
def brightness(self, brightness):
159+
def brightness(self, brightness: int):
154160
if not 0 <= brightness <= 15:
155161
raise ValueError("Brightness must be a value between 0 and 15")
156162
self._matrix.brightness = brightness / 15

0 commit comments

Comments
 (0)