From 6480ca4ac2bf413cd434213a64e7e485c02e01d5 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 10 Nov 2021 20:55:24 -0500 Subject: [PATCH 1/3] Added type hints --- adafruit_pixelbuf.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/adafruit_pixelbuf.py b/adafruit_pixelbuf.py index 47596a9..5e22bb4 100644 --- a/adafruit_pixelbuf.py +++ b/adafruit_pixelbuf.py @@ -13,6 +13,12 @@ """ +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 +43,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 +100,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. @@ -153,7 +159,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 +202,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 +214,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 +264,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 +295,7 @@ 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 +308,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 +328,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 +342,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") From 41067e6de869916035627e7f108d8b90d7d3e559 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 10 Nov 2021 20:55:32 -0500 Subject: [PATCH 2/3] Fixed docstring --- adafruit_pixelbuf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_pixelbuf.py b/adafruit_pixelbuf.py index 5e22bb4..48066fd 100644 --- a/adafruit_pixelbuf.py +++ b/adafruit_pixelbuf.py @@ -112,8 +112,8 @@ def parse_byteorder(byteorder: str) -> Tuple[int, str, bool, bool]: 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 From 8701a8e59685db7f51efe3bf422ebf50ff4e4e0d Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Wed, 10 Nov 2021 20:58:48 -0500 Subject: [PATCH 3/3] Reformatted per pre-commit --- adafruit_pixelbuf.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_pixelbuf.py b/adafruit_pixelbuf.py index 48066fd..2c31786 100644 --- a/adafruit_pixelbuf.py +++ b/adafruit_pixelbuf.py @@ -15,6 +15,7 @@ try: from typing import Optional, Tuple, Union, Sequence + ColorUnion = Union[int, Tuple[int, int, int], Tuple[int, int, int, int]] except ImportError: pass @@ -295,7 +296,9 @@ def _set_item( b * self._brightness ) - def __setitem__(self, index: Union[int, slice], val: Union[ColorUnion, Sequence[ColorUnion]]): + 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)):