Skip to content

Add typehints #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 5, 2021
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
name: pylint (library code)
types: [python]
args:
- --disable=consider-using-f-string
- --disable=consider-using-f-string,duplicate-code
exclude: "^(docs/|examples/|tests/|setup.py$)"
- id: pylint
name: pylint (example code)
Expand Down
10 changes: 8 additions & 2 deletions adafruit_featherwing/alphanum_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

import board
import adafruit_ht16k33.segments as segments
from adafruit_ht16k33 import segments
from adafruit_featherwing.led_segments import Segments

try:
from typing import Optional
from busio import I2C
except ImportError:
pass


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

def __init__(self, address=0x70, i2c=None):
def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None):
super().__init__()
if i2c is None:
i2c = board.I2C()
Expand Down
2 changes: 1 addition & 1 deletion adafruit_featherwing/auto_writeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def auto_write(self):
return self._auto_write

@auto_write.setter
def auto_write(self, write):
def auto_write(self, write: bool):
if isinstance(write, bool):
self._auto_write = write
10 changes: 9 additions & 1 deletion adafruit_featherwing/dotstar_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@
import adafruit_dotstar as dotstar
from adafruit_featherwing.pixelmatrix import PixelMatrix

try:
import typing # pylint: disable=unused-import
from microcontroller import Pin
except ImportError:
pass


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

def __init__(self, clock=board.D13, data=board.D11, brightness=0.2):
def __init__(
self, clock: Pin = board.D13, data: Pin = board.D11, brightness: float = 0.2
):
"""
:param pin clock: The clock pin for the featherwing
:param pin data: The data pin for the featherwing
Expand Down
16 changes: 10 additions & 6 deletions adafruit_featherwing/gps_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
import busio
import adafruit_gps

try:
from typing import Optional
except ImportError:
pass


class GPSFeatherWing:
"""Class representing an `Ultimate GPS FeatherWing
<https://www.adafruit.com/product/3133>`_.

Automatically uses the feather's UART bus."""

def __init__(self, update_period=1000, baudrate=9600):
def __init__(self, update_period: int = 1000, baudrate: int = 9600):
"""
:param int update_period: (Optional) The amount of time in milliseconds between
updates (default=1000)
Expand All @@ -36,8 +41,7 @@ def __init__(self, update_period=1000, baudrate=9600):
if update_period < 250:
raise ValueError("Update Frequency be at least 250 milliseconds")
timeout = update_period // 1000 + 2
if timeout < 3:
timeout = 3
timeout = max(timeout, 3)

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

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

def read(self, size):
def read(self, size: int) -> Optional[bytearray]:
"""
Read the UART for any information that may be on it

Expand All @@ -69,7 +73,7 @@ def read(self, size):
return self._uart.read(size)
return None

def send_command(self, command):
def send_command(self, command: bytearray):
"""
Send a bytearray command to the GPS module

Expand Down
8 changes: 7 additions & 1 deletion adafruit_featherwing/ina219_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
import board
import adafruit_ina219

try:
from typing import Optional
from busio import I2C
except ImportError:
pass


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

def __init__(self, i2c=None):
def __init__(self, i2c: Optional[I2C] = None):
if i2c is None:
i2c = board.I2C()
self._ina219 = adafruit_ina219.INA219(i2c)
Expand Down
13 changes: 10 additions & 3 deletions adafruit_featherwing/joy_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
from micropython import const
import adafruit_seesaw.seesaw

try:
from typing import Optional, Tuple
from busio import I2C
except ImportError:
pass


BUTTON_A = const(1 << 6)
BUTTON_B = const(1 << 7)
BUTTON_Y = const(1 << 9)
Expand All @@ -30,7 +37,7 @@ class JoyFeatherWing:

Automatically uses the feather's I2C bus."""

def __init__(self, i2c=None):
def __init__(self, i2c: Optional[I2C] = None):
if i2c is None:
i2c = board.I2C()
self._seesaw = adafruit_seesaw.seesaw.Seesaw(i2c)
Expand Down Expand Up @@ -157,7 +164,7 @@ def button_select(self):
"""
return self._check_button(BUTTON_SELECT)

def _check_button(self, button):
def _check_button(self, button: int) -> bool:
"""Utilises the seesaw to determine which button is being pressed."""
buttons = self._seesaw.digital_read_bulk(button)
return not buttons != 0
Expand Down Expand Up @@ -200,7 +207,7 @@ def joystick_offset(self):
return self._joystick_offset

@joystick_offset.setter
def joystick_offset(self, offset):
def joystick_offset(self, offset: Tuple[int, int]):
self._joystick_offset = offset

def zero_joystick(self):
Expand Down
21 changes: 14 additions & 7 deletions adafruit_featherwing/keyboard_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
# pylint: disable-msg=too-many-arguments
from adafruit_featherwing.tft_featherwing import TFTFeatherWing

try:
from typing import Optional
from busio import SPI, I2C
from microcontroller import Pin
except ImportError:
pass


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

def __init__(
self,
spi=None,
cs=None,
dc=None,
i2c=None,
ts_cs=None,
sd_cs=None,
neopixel_pin=None,
spi: Optional[SPI] = None,
cs: Optional[Pin] = None,
dc: Optional[Pin] = None,
i2c: Optional[I2C] = None,
ts_cs: Optional[Pin] = None,
sd_cs: Optional[Pin] = None,
neopixel_pin: Optional[Pin] = None,
):
super().__init__(spi, cs, dc, ts_cs, sd_cs)

Expand Down
15 changes: 10 additions & 5 deletions adafruit_featherwing/led_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

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

try:
from typing import Union
except ImportError:
pass


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

def print(self, value):
def print(self, value: Union[str, int]):
"""
Print a number or text to the display
Expand All @@ -41,7 +46,7 @@ def print(self, value):
self._segments.print(value)
self._segments.show()

def marquee(self, text, delay=0.25, loop=True):
def marquee(self, text: str, delay: float = 0.25, loop: bool = True):
"""
Automatically scroll the text at the specified delay between characters
Expand All @@ -53,7 +58,7 @@ def marquee(self, text, delay=0.25, loop=True):
"""
self._segments.marquee(text, delay, loop)

def fill(self, fill):
def fill(self, fill: bool):
"""Change all Segments on or off
:param bool fill: True turns all segments on, False turns all segments off
Expand All @@ -75,7 +80,7 @@ def blink_rate(self):
return self._segments.blink_rate

@blink_rate.setter
def blink_rate(self, rate):
def blink_rate(self, rate: int):
self._segments.blink_rate = rate

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

@brightness.setter
def brightness(self, brightness):
def brightness(self, brightness: int):
if not 0 <= brightness <= 15:
raise ValueError("Brightness must be a value between 0 and 15")
self._segments.brightness = brightness / 15
30 changes: 18 additions & 12 deletions adafruit_featherwing/matrix_featherwing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

import board
import adafruit_ht16k33.matrix as matrix
from adafruit_ht16k33 import matrix

from adafruit_featherwing.auto_writeable import AutoWriteable

try:
from typing import Optional, Tuple, Union
from busio import I2C
except ImportError:
pass


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

def __init__(self, address=0x70, i2c=None):
def __init__(self, address: int = 0x70, i2c: Optional[I2C] = None):

if i2c is None:
i2c = board.I2C()
Expand All @@ -37,14 +43,14 @@ def __init__(self, address=0x70, i2c=None):
self.rows = 8
super().__init__()

def __getitem__(self, key):
def __getitem__(self, key: Tuple[int, int]) -> bool:
"""
Get the current value of a pixel
"""
x, y = key
return self.pixel(x, y)

def __setitem__(self, key, value):
def __setitem__(self, key: Tuple[int, int], value: Union[int, bool]):
"""
Turn a pixel off or on
"""
Expand All @@ -59,7 +65,7 @@ def _update(self):
if self._auto_write:
self._matrix.show()

def pixel(self, x, y, color=None):
def pixel(self, x: int, y: int, color: Optional[bool] = None) -> Optional[bool]:
"""
Turn a pixel on or off or retrieve a pixel value
Expand All @@ -79,7 +85,7 @@ def show(self):
"""
self._matrix.show()

def fill(self, fill):
def fill(self, fill: bool):
"""
Turn all pixels on or off
Expand All @@ -92,7 +98,7 @@ def fill(self, fill):
else:
raise ValueError("Must set to either True or False.")

def shift_right(self, rotate=False):
def shift_right(self, rotate: bool = False):
"""
Shift all pixels right
Expand All @@ -101,7 +107,7 @@ def shift_right(self, rotate=False):
self._matrix.shift_right(rotate)
self._update()

def shift_left(self, rotate=False):
def shift_left(self, rotate: bool = False):
"""
Shift all pixels left
Expand All @@ -110,7 +116,7 @@ def shift_left(self, rotate=False):
self._matrix.shift_left(rotate)
self._update()

def shift_up(self, rotate=False):
def shift_up(self, rotate: bool = False):
"""
Shift all pixels up
Expand All @@ -119,7 +125,7 @@ def shift_up(self, rotate=False):
self._matrix.shift_up(rotate)
self._update()

def shift_down(self, rotate=False):
def shift_down(self, rotate: bool = False):
"""
Shift all pixels down
Expand All @@ -138,7 +144,7 @@ def blink_rate(self):
return self._matrix.blink_rate

@blink_rate.setter
def blink_rate(self, rate):
def blink_rate(self, rate: int):
self._matrix.blink_rate = rate

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

@brightness.setter
def brightness(self, brightness):
def brightness(self, brightness: int):
if not 0 <= brightness <= 15:
raise ValueError("Brightness must be a value between 0 and 15")
self._matrix.brightness = brightness / 15
Loading