Skip to content

Add Type Hints #3

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 5 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions adafruit_pixelbuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)):
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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")
6 changes: 6 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2021 Alec Delaney
#
# SPDX-License-Identifier: MIT

sphinx>=4.0.0
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
# SPDX-License-Identifier: MIT

Adafruit-Blinka
sphinx>=4.0.0