Skip to content

Commit 3c88501

Browse files
authored
Merge pull request #3 from tekktrik/feature/add-typing
Add Type Hints
2 parents edee62c + 3caa8fe commit 3c88501

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

adafruit_pixelbuf.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
1414
"""
1515

16+
try:
17+
from typing import Optional, Tuple, Union, Sequence
18+
19+
ColorUnion = Union[int, Tuple[int, int, int], Tuple[int, int, int, int]]
20+
except ImportError:
21+
pass
22+
1623
__version__ = "0.0.0-auto.0"
1724
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Pixelbuf.git"
1825

@@ -37,12 +44,12 @@ class PixelBuf: # pylint: disable=too-many-instance-attributes
3744

3845
def __init__( # pylint: disable=too-many-locals,too-many-arguments
3946
self,
40-
n,
41-
byteorder="BGR",
42-
brightness=1.0,
43-
auto_write=False,
44-
header=None,
45-
trailer=None,
47+
n: int,
48+
byteorder: str = "BGR",
49+
brightness: float = 1.0,
50+
auto_write: bool = False,
51+
header: Optional[bytes] = None,
52+
trailer: Optional[bytes] = None,
4653
):
4754

4855
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
94101
self.auto_write = auto_write
95102

96103
@staticmethod
97-
def parse_byteorder(byteorder):
104+
def parse_byteorder(byteorder: str) -> Tuple[int, str, bool, bool]:
98105
"""
99106
Parse a Byteorder string for validity and determine bpp, byte order, and
100107
dostar brightness bits.
@@ -106,8 +113,8 @@ def parse_byteorder(byteorder):
106113
W - White
107114
P - PWM (PWM Duty cycle for pixel - dotstars 0 - 1.0)
108115
109-
:param: ~str bpp: bpp string.
110-
:return: ~tuple: bpp, byteorder, has_white, dotstar_mode
116+
:param ~str bpp: bpp string.
117+
:return ~tuple: bpp, byteorder, has_white, dotstar_mode
111118
"""
112119
bpp = len(byteorder)
113120
dotstar_mode = False
@@ -153,7 +160,7 @@ def brightness(self):
153160
return self._brightness
154161

155162
@brightness.setter
156-
def brightness(self, value):
163+
def brightness(self, value: float):
157164
value = min(max(value, 0.0), 1.0)
158165
change = value - self._brightness
159166
if -0.001 < change < 0.001:
@@ -196,7 +203,7 @@ def show(self):
196203
"""
197204
return self._transmit(self._post_brightness_buffer)
198205

199-
def fill(self, color):
206+
def fill(self, color: ColorUnion):
200207
"""
201208
Fills the given pixelbuf with the given color.
202209
:param pixelbuf: A pixel object.
@@ -208,7 +215,7 @@ def fill(self, color):
208215
if self.auto_write:
209216
self.show()
210217

211-
def _parse_color(self, value):
218+
def _parse_color(self, value: ColorUnion) -> Tuple[int, int, int, int]:
212219
r = 0
213220
g = 0
214221
b = 0
@@ -258,7 +265,7 @@ def _parse_color(self, value):
258265
return (r, g, b, w)
259266

260267
def _set_item(
261-
self, index, r, g, b, w
268+
self, index: int, r: int, g: int, b: int, w: int
262269
): # pylint: disable=too-many-locals,too-many-branches,too-many-arguments
263270
if index < 0:
264271
index += len(self)
@@ -289,7 +296,9 @@ def _set_item(
289296
b * self._brightness
290297
)
291298

292-
def __setitem__(self, index, val):
299+
def __setitem__(
300+
self, index: Union[int, slice], val: Union[ColorUnion, Sequence[ColorUnion]]
301+
):
293302
if isinstance(index, slice):
294303
start, stop, step = index.indices(self._pixels)
295304
for val_i, in_i in enumerate(range(start, stop, step)):
@@ -302,7 +311,7 @@ def __setitem__(self, index, val):
302311
if self.auto_write:
303312
self.show()
304313

305-
def _getitem(self, index):
314+
def _getitem(self, index: int):
306315
start = self._offset + (index * self._bpp)
307316
buffer = (
308317
self._pre_brightness_buffer
@@ -322,7 +331,7 @@ def _getitem(self, index):
322331
)
323332
return value
324333

325-
def __getitem__(self, index):
334+
def __getitem__(self, index: Union[int, slice]):
326335
if isinstance(index, slice):
327336
out = []
328337
for in_i in range(
@@ -336,5 +345,5 @@ def __getitem__(self, index):
336345
raise IndexError
337346
return self._getitem(index)
338347

339-
def _transmit(self, buffer):
348+
def _transmit(self, buffer: bytearray):
340349
raise NotImplementedError("Must be subclassed")

0 commit comments

Comments
 (0)