diff --git a/adafruit_pixelbuf.py b/adafruit_pixelbuf.py index 47596a9..2c31786 100644 --- a/adafruit_pixelbuf.py +++ b/adafruit_pixelbuf.py @@ -13,6 +13,13 @@ """ +try: + from typing import Optional, Tuple, Union, Sequence + + ColorUnion = Union[int, Tuple[int, int, int], Tuple[int, int, int, int]] +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Pixelbuf.git" @@ -37,12 +44,12 @@ class PixelBuf: # pylint: disable=too-many-instance-attributes def __init__( # pylint: disable=too-many-locals,too-many-arguments self, - n, - byteorder="BGR", - brightness=1.0, - auto_write=False, - header=None, - trailer=None, + n: int, + byteorder: str = "BGR", + brightness: float = 1.0, + auto_write: bool = False, + header: Optional[bytes] = None, + trailer: Optional[bytes] = None, ): bpp, byteorder_tuple, has_white, dotstar_mode = self.parse_byteorder(byteorder) @@ -94,7 +101,7 @@ def __init__( # pylint: disable=too-many-locals,too-many-arguments self.auto_write = auto_write @staticmethod - def parse_byteorder(byteorder): + def parse_byteorder(byteorder: str) -> Tuple[int, str, bool, bool]: """ Parse a Byteorder string for validity and determine bpp, byte order, and dostar brightness bits. @@ -106,8 +113,8 @@ def parse_byteorder(byteorder): W - White P - PWM (PWM Duty cycle for pixel - dotstars 0 - 1.0) - :param: ~str bpp: bpp string. - :return: ~tuple: bpp, byteorder, has_white, dotstar_mode + :param ~str bpp: bpp string. + :return ~tuple: bpp, byteorder, has_white, dotstar_mode """ bpp = len(byteorder) dotstar_mode = False @@ -153,7 +160,7 @@ def brightness(self): return self._brightness @brightness.setter - def brightness(self, value): + def brightness(self, value: float): value = min(max(value, 0.0), 1.0) change = value - self._brightness if -0.001 < change < 0.001: @@ -196,7 +203,7 @@ def show(self): """ return self._transmit(self._post_brightness_buffer) - def fill(self, color): + def fill(self, color: ColorUnion): """ Fills the given pixelbuf with the given color. :param pixelbuf: A pixel object. @@ -208,7 +215,7 @@ def fill(self, color): if self.auto_write: self.show() - def _parse_color(self, value): + def _parse_color(self, value: ColorUnion) -> Tuple[int, int, int, int]: r = 0 g = 0 b = 0 @@ -258,7 +265,7 @@ def _parse_color(self, value): return (r, g, b, w) def _set_item( - self, index, r, g, b, w + self, index: int, r: int, g: int, b: int, w: int ): # pylint: disable=too-many-locals,too-many-branches,too-many-arguments if index < 0: index += len(self) @@ -289,7 +296,9 @@ def _set_item( b * self._brightness ) - def __setitem__(self, index, val): + def __setitem__( + self, index: Union[int, slice], val: Union[ColorUnion, Sequence[ColorUnion]] + ): if isinstance(index, slice): start, stop, step = index.indices(self._pixels) for val_i, in_i in enumerate(range(start, stop, step)): @@ -302,7 +311,7 @@ def __setitem__(self, index, val): if self.auto_write: self.show() - def _getitem(self, index): + def _getitem(self, index: int): start = self._offset + (index * self._bpp) buffer = ( self._pre_brightness_buffer @@ -322,7 +331,7 @@ def _getitem(self, index): ) return value - def __getitem__(self, index): + def __getitem__(self, index: Union[int, slice]): if isinstance(index, slice): out = [] for in_i in range( @@ -336,5 +345,5 @@ def __getitem__(self, index): raise IndexError return self._getitem(index) - def _transmit(self, buffer): + def _transmit(self, buffer: bytearray): raise NotImplementedError("Must be subclassed")